- Home /
NullReferenceException after passing InputField gameObject into EventSystem.SetSelectedGameObject()
For some reason InputManager settings are ignored, so I've implemented a script to navigate UI elements. There's an idea:
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);
}
}
It works when current
contains Buttons or Toggles, but when it is InpuField navigation breaks and log gives me following error every frame.
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)
Answer by Destolos · Feb 08, 2020 at 08:23 AM
Hello,
I had the same problem. I found out that in my case the currentInputModule (https://docs.unity3d.com/2018.1/Documentation/ScriptReference/EventSystems.EventSystem-currentInputModule.html) of my current EventSystem (https://docs.unity3d.com/2018.1/Documentation/ScriptReference/EventSystems.EventSystem-current.html) was null.
Your answer
Follow this Question
Related Questions
Selecting a selectable using script doesn't seem to work anymore? 1 Answer
keyboard submit not working on selectables. 1 Answer
Adding variables to Input Field and selecting an Input Field 1 Answer
Automatic navigation does not work with Inherited InputField 1 Answer
Unity InputField UI Not Responsive 0 Answers