- Home /
The question is answered, right answer was accepted
Why is GetComponent() causing a null exception
Hello! This line in my code
GetComponent<EditorGUI>().bMouseOverGUI = false;
and every other GetComponent() call I do causisi this runtime error : NullReferenceException: Object reference not set to an instance of an object.
Does anyone know why this happens? Thanks.
Answer by randomuser · Apr 03, 2013 at 11:38 PM
The problem was that I was calling the script during Start(). Thanks!
Answer by Eric5h5 · Dec 03, 2012 at 04:25 AM
Do you actually have a script called EditorGUI? If so, you shouldn't call it that, since it's the name of one of Unity's classes.
And it won't work at runtime since this particular class is inside the UnityEditor assembly.
Yes, I do have a class called EditorGUI, but the problem persists with another class as well which I tried.
unless Leval$$anonymous$$anager is also a unity system class
Did you make sure that your object does have this component? It could be on another object or on a child object.
Are you sure that b$$anonymous$$ouseOverGUI is a public bool?
Follow this Question
Related Questions
Disabling A Script on a GameObject From a Different Script 2 Answers
Can't access Depth of Field? 2 Answers
Null Reference Exception on instantiated object's script. 2 Answers
Selecting Object From Top Causes NullReference 1 Answer
How do I fix UnityEngine.Transform' does not contain a definition for `GameObject'? 2 Answers