- Home /
Scriptable Object and Polymorphism
So I have a scriptable object which has a list of BaseClass. However, there are other classes that I wrote which inherits from the BaseClass. So I decided to use Polymorphism and shove everything in that list. For a second, I thought everything was going fine... BUT, after I restarted unity, all the objects that were inherited from the base class turned into base classes. (I cant type cast it, and gives me an error "cannot cast from source type to destination type")
I suspect this is a problem from serialization or a problem on how I import my objects.. This is how I import it:
itemListData = (ItemDatabase)AssetDatabase.LoadAssetAtPath(currentItemListLocation, typeof(ItemDatabase));
And this is my database class
public class ItemDatabase : ScriptableObject {
public List<BaseClass> itemList = new List<BaseClass>();
}
I believe that when you do LoadAssetAtPath(path, typeof(ItemDatabase)), you're telling Unity the exact type that it should load the object as.
There's a problem with Unity not serializing polymorphic classes very well (ie. if you have a list of Animals, and put Cats and Dogs in there, they'll come out of serialization as just Animals), but using ScriptableObjects is one of the ways you get around that problem. I use serialized scriptable objects of derived types all the time, but those are linked directly on objects, not loaded from resources, and in that case everything works fine.
Try to load with the exact type ins$$anonymous$$d of the base type, and see if that's a possible solution.
Thank you for the answer.
I am using scriptable object to store a list of Item. Item is a class and there are a lot of classes inherited to Item(Some of them are inherited to these subclasses).
Also it would be hard to have lists for every type since I have at least 10 different classes inheriting from the base class. Are there any solutions to serialization and polymorphism? >.<
Answer by Skrimel · Jun 05, 2020 at 08:20 PM
Late answer, but in modern versions of Unity you can find SerializeReferenceAttribute (https://docs.unity3d.com/ScriptReference/SerializeReference.html), which will help to serialize entities with support of their polymorphism. Also look at serialization rules and read all restrictions patiently. There are some undocumented features/bugs in serialization system, but in the most cases you won't even feel them.
Also, one weird "bug": Unity support SerializeReferenceAttribute for use with fields of type, that is a subclass of a UnityEngine.Object, but it leads to memory "leaks" - everyrhing works fine, except it takes much more time for hot reloads, that are very often in work process in Unity.
Your answer
![](https://koobas.hobune.stream/wayback/20220613194048im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
An OS design issue: File types associated with their appropriate programs 1 Answer
passing paramters to a subclass? c# 2 Answers
Character Class Hierarchy with soldier classes and friendly/enemy character 1 Answer
Draw Inspector For Most Derived Type Of Collection Elements 3 Answers
Polymorphic weapon system 1 Answer