- Home /
Abstract classes in a ScriptableObject
I'm trying to make a save system with ScriptableObjects. I have the class SaveData, which inherits from ScriptableObject, that holds several other classes meant to hold different kinds of data. One of them - SaveStateHolder - takes care of data that may break as a result of an update to the game. This class has a List that holds that data. SaveState is an abstract class that doesn't inherit from anything and has several other classes that inherit from it.
When I try to add an instance of one such class to the list while in play mode, eg, AchievementSaveState, it's no longer there when I exit play mode.
All classes referenced inside SaveData (including SaveStateHolder, SaveState and all of its children) are marked as serializable. SaveData has been instanced as an asset via the CreateAssetMenu attribute, and all the data I mentioned goes inside that instance.
Why is this happening? How can I fix it? I know Unity has problems with serializing abstract classes, but I don't know any other way I could do this.
Answer by Bunny83 · Jan 25, 2021 at 06:37 PM
Well, Unity's serialization system did not support inheritance for custom serializable classes. Only ScriptableObject derived classes did support inheritance. However there was an addition to the serialization system to allow things being serialized with their type. This requires the field to have the SerializeReference attribute. Note that fields with that attribute of course requires custom editor handling as the Unity inspector only has limited support for those.
Your answer
Follow this Question
Related Questions
Save EditorWindow to disk 1 Answer
ISerializationCallbackReceiver not working on ScriptableObject 2 Answers
Scriptable Object List values are always null 1 Answer
Unity Not Loading 0 Answers
Getting any type in Unity Inspector 1 Answer