将InputField游戏对象传递到EventSystem.SetSelectedGameObject()后,Unity 5.6 NullReferenceException

时间:2018-06-20 07:45:47

标签: c# unity3d unity3d-ui

由于某些原因,InputManager设置将被忽略,因此我实现了一个脚本来导航UI元素。有一个主意:

Selectable current

void setCurrent(Selectable selectable) {
      current = selectable;
      EventSystem.current.SetSelectedGameObject(current.gameObject, null);
}

void Update(){
      if(current == null) {
          setCurrent(Selectable.allSelectables[0]);
      }

      if(Input.GetKeyDown(KeyCode.UpArrow) && current.navigation.selectOnUp != null) {
          setCurrent(current.navigation.selectOnUp);
      }
}

current包含“按钮”或“切换”时有效,但是当InpuField导航中断并且日志显示每帧时出现以下错误。

NullReferenceException: Object reference not set to an instance of an object
UnityEngine.UI.InputField.ActivateInputFieldInternal () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/InputField.cs:2213)
UnityEngine.UI.InputField.LateUpdate () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/InputField.cs:725)

[编辑]

我确定current永远不会为空。问题不是关于NullReferenceException是什么,而是为什么某些源自Selectable的对象会导致该错误。我希望一些统一的开发人员可以重现此错误。

0 个答案:

没有答案