- Home /
Problem is not reproducible or outdated
InputField.ActivateInputField() giving NullReferenceException error
I have an input field (for a chat), and when enable the panel that the Input Field is in, I try to use InputField.ActivateInputField(); so that it can be immediately typed in to. That line returns this error:
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:1881)
UnityEngine.UI.InputField.LateUpdate () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/InputField.cs:519)
For some weird reason it worked the first time, then just quit working. I'm using Unity 5.0.0p3
EDIT: I also just installed Unity 5.0.1f1 and it still gives me the same error.
The ones mentioned in the error are Unity's input field scripts. "C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/InputField.cs"
I already saw what you replied. I am asking for the code at Line No. 519 and line No.1881 of InputField.cs
Answer by DiegoSLTS · Apr 02, 2015 at 07:38 PM
I just tested calling that method (ActivateInputField) multiple times per frame on an input field and it works. You probably have some other code that's changing something from the InputField variable, but it's hard to know since you didn't share relevant information.
Share the full script that contains that InputField variable and any other script related. Also, explain or upload a screenshot of the inspector of the objects related to this.
One last thing, the latest official Unity release is 5.0.0f4, try to install the latest one unless you have a reason to use f1.
I just tried out a couple things, and it works fine if the scene is just there. But if I load another scene (additively) it gives me the error.
So if I load the scene on its own, it activates the field fine. But that's not the way I want it to work
P.S. I said 5.0.1 not 5.0.0 :P
What does your added scene stuff do? Does something change the inputfield or the object that has the component with the script that uses it? Is the object with that component a singleton maybe? It's easy to mess things up with singletons.
So the starting scene (main menu) has a script (to hold variables when joining a server) and a GameObject that holds all of the GameObjects in the scene excluding the script. It holds GUI, the background, EventSystem, etc. Then I load the game scene additively and destroy the GameObject that was holding all of the stuff from the main menu.
I don't know what a singleton is so there probably are none.
You're still sharing too few info, it's really hard to help you with this. Please, share scripts, describe what components are attached to the game objects, everything is too vague.
It would be ideal if you can share the whole project, or at least a $$anonymous$$imal project that reproduces your problem.
I can only guess that you're destroying the inputfield that's referenced in some script, but you're not destroying the object that has that script. At some point that object tries to execute the ActiveInputField method on the destroyed inputfield so it fails.
I ended up changing some things around so that I can load the new level normally ins$$anonymous$$d of additively, so it works fine now.
Follow this Question
Related Questions
Text.text does not update 0 Answers
Missing Input Field Cursor on Web Player 2 Answers
Hide Textselection in Scrollable Inputfield 1 Answer
IME(Input Method Editor) Chinese Pin yin 0 Answers