Nullable Serialized Custom Classes?

Hello I often encounter situations where it would be useful to be able to Serialize a custom class so that it appears in the inspector, BUT to also have it start off null. That is to say, you have to actively declare it a new MyClass() or click the + on the inspector to use it.

Is there an easy way to do this?

Yes, there is and it’s called SerializeReference.
Though be warned that while the Inspector will show the serialized class out-of-the-box, there is currently no built-in way to create a particular instance. Since SerializeReference does support polymorphism, you need some custom editor code to create an instance or to set it back to null.

You may create your own PropertyDrawer to handle the cases you need or you have to look for custom solutions by someone else that suits your needs.

Ah that seems like a lot of fiddling for something that might not work properly. Am I reading that right?

It can be done and will work, the question is do you really need this or is there an easier way to get to your objective?

1 Like

It would be very helpful to have. It would also reduce the data attached to each object. But not vital. I can work round it.

I have an example of a property drawer for SerializeReference here: abstract SkillList that can be edited in Inspector - #15 by spiney199

There are other free plugins out there, though Odin Inspector is the best choice for the best ‘out of the box’ support for SerializeReference, while being paid.

1 Like

Thank you Spins

I take this as Spiney’s answer has answered your question. So I took the liberty and marked his answer as the solution. I guess you should be able to change it if you want to, but I assume that’s fine?

I haven’t tried it yet so maybe a little premature.