【发布时间】: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