Skip to main content
gamedev.net
🔒 Locked

NullReferenceException Unity, why?

Started by retrometron 3 replies 2.5k views
Original Post
retrometron
retrometron

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?

Kylotan
Kylotan

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.

retrometron
retrometron

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

retrometron
retrometron

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.


Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.