Custom editor : object are saved as their inherited class instead of their actual class type

Hi,

I’m facing a problem and I can’t find out a solution.

In a custom editor I have something like List items. In that list I can add items that are not BasicItem but something like FirItem, WaterItem etc. which inherited from BasicItem. So far there is no problem and the Custom Editor show me for each item all BasicItem properties + the specific item type properties - great !

But if I close and re-open my project the custom editor will only show BasicItem properties for each items, even if the item is supposed to be FireItem or WaterItem etc.

I’m new to CustomEditor - my guess is that Unity save all items with the declared type of the List - so it converts all items to BasicItem. Is that right ? Is there a solution to avoid that ? I need all the items in a same List because the order matters, I don’t want to create a List for each specific item type.

Thanks

Hi,

After some research I tried to make BasicItem inherits from ScriptableObject - but things got worse, each item was considered as not an instance by Unity after close/open the project. I tried to SetDirty the item instance when is created but it changed nothing.

Any idea guys ?

Thanks

Hey there,

Unity does not support polymorphic serialization. You can’t serialize something as a base class and expect to get the inheriting class back. There is no way around this unless you serialize references (ie ScriptableObjects).

Cheers,

@Kiupe If this is your issue, a vote or 2 on this feedback item would be good. It’s long overdue.