【问题标题】:NullReferenceExceptionwhen using Unity UIBuilder drop down menu使用 Unity UI Builder 下拉菜单时出现 NullReferenceException
【发布时间】:2023-01-19 04:12:58
【问题描述】:

我不断收到以下错误: “NullReferenceException:对象引用未设置到对象的实例”

我几乎完全复制了示例代码,但在尝试更改选择列表时似乎一直出现此错误,这是我针对此元素的代码(抱歉,我是 unity ui 的新手):

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
using UnityEngine.SceneManagement;
using UnityEngine.Audio;

public class SettingsMenuController : MonoBehaviour
{
    public DropdownField ResolutionSelect;

    [SerializeField] public List<string> Resolutions = new List<string> {"Option1","Option2","Option45"};

    // Start is called before the first frame update. 
    void Start()
    {
        var root = GetComponent<UIDocument>().rootVisualElement;

        ResolutionSelect = root.Q<DropdownField>("ResolutionSelect");

        ResolutionSelect.choices = new List<string> {"option1"};
        ResolutionSelect.value = Resolutions[0];
    }
}

错误发生在“ResolutionSelect.choices = new List {“option1”};”行。

我正在尝试制作一个下拉菜单来更改分辨率,但事实证明这很困难,我们感谢您提供任何帮助或建议,并在此先感谢您。

【问题讨论】:

  • 您提到您“几乎完全复制了示例代码”。请参考您复制的内容和您主动更改的内容。
  • 我使用了 Window>UI Toolkit>Samples 中的示例

标签: unity3d user-interface unity-ui


【解决方案1】:

(很抱歉为这条评论写一个答案,但我没有足够的声誉来实际评论 -.-)

您找到解决方案了吗?

我有完全相同的问题......

调试了一下后,我发现这可能是一个问题

ResolutionSelect = root.Q("ResolutionSelect");

当我记录刚刚查询的下拉列表的文本时:

Debug.Log(ResolutionSelect.text);

它只是空的。

所以可能查询不起作用或找不到实际的 DropDown?

【讨论】:

    猜你喜欢
    • 2014-03-25
    • 1970-01-01
    • 1970-01-01
    • 2020-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-25
    • 1970-01-01
    相关资源
    最近更新 更多