- Home /
NullReferenceException when looking at target in an Editor class (CustomEditor)
Hi. I have some code that worked fine in Unity 3, and now constantly throws Exceptions in Unity 4. I suspect this is a bug.
If I even look at the value of target in my CustomEditor class, then a NRE happens, which shouldn't happen. It appears to go through a broken accessor in Unity's internal code.
The specific error is:
NullReferenceException: Object reference not set to an instance of an object
UnityEditor.Editor.get_referenceTargetIndex ()
UnityEditor.Editor.get_target ()
LDLevelObjectEditor.OnEnable () (at Assets/Editor/LDLevelObjectEditor.cs:63)
Line 63 in LDLevelObjectEditor is in OnEnable():
//line 63 is the one directly below
LDLevelObject o = target as LDLevelObject;
if ( o == null )
{
return;
}
You can see I even escape out if target does not exist for some reason. But the very nature of trying to access the target causes a NRE. I'm thinking it's trying to get the target from a null GameObject, but that shouldn't be possible if Unity is working correctly.
Also it's worth noting that this happens during gameplay, which should never be the case since this is an Editor class.
Anybody encountered this and maybe know of a fix?
Have you solve it yet?
I'm having the same problem (Windows, Unity 3.5.6f6).
It happens after the assembly reload (while in Play mode), and there seems the Inspector renders nothing, although the selection (in the Hierarchy) window remains after the reload.
The exception is being continuously thrown, until I select another object in the hierarchy window - then it stops (and the Inspector is being redrawn properly).
Also, I noticed that this doesn't happen always, but in a 90% of cases.
And yes, it is related to "reading" the target property of the Editor class.
Thanks for the answer!
Answer by dkozar · May 25, 2013 at 04:29 PM
Just found out that it happens ONLY when recompiling my custom assembly containing the editor code.
Somehow I managed to get a proper error message, which was:
"Instance of StageEditor couldn't be created because there is no script with that name."
("StageEditor" is the name of my custom editor)
This doesn't happen if I don't touch the editor code, but only changing scripts that have nothing to do with the editor (their change doesn't require the Custom editor assembly rebuild). The assembly reloads normally, the Inspector renders OK, and there's no the "target" problem.
Guess I could live with that (final users won't see this error) :)
I got this to go away, but I honestly can't recall how. Hmmm...
Answer by Hexane · Aug 12, 2021 at 03:12 PM
I just want to add that this bug is still happening in Unity 2020.3.15.F2 and this is the top search result for me. It went away after restarting the Unity Editor. ✌
Your answer
Follow this Question
Related Questions
Unity Inspector Interface 5 Answers
Unity 5 editor flickering/glitchy (examples inside) 3 Answers
Strange graphical bug in Editor GUI 1 Answer
DrawDefaultInspector crash 1 Answer