Advertisement

NullReferenceException Unity, why?

Started by June 07, 2018 01:38 PM
3 comments, last by retrometron 6 years, 6 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?

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.

Advertisement

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