NullReferenceException Unity, why?

Started by
3 comments, last by retrometron 5 years, 10 months ago

I have this code in Unity in an custom inspector in the method OnInspectorGUI():


MyCustomMonoBehaviour mono = target as MyCustomMonoBehaviour; 

List<Operation> myOperations = mono.operations;
myOperations.add(new Operation());

And I get this error, I don't know why:

> NullReferenceException: Object > reference not set to an instance of an > object MyTestEditor.OnInspectorGUI () 

Any idea?

Advertisement

You would see this if 'target' is null, or 'target' is not actually a MyCustomMonoBehaviour, or target.operations was null. Use the debugger or Debug.Log to find out which.

yeah, thanks Kylotan, target seems to be null :O... 

I fixed :), I initialized in OnEnable() and it's working fine, thanks very much.

A question, why do the people prefer to use new SerializedObject(target) instead?. That is how I saw in other examples.

 

This topic is closed to new replies.

Advertisement