Serialize Lists with multiple inherited classes or interfaces ?
Hello there ^_^
I'm trying to Serialize a List with objects from different kind of classes that inherit from one base class. This list should allow me both to see all the objects and their serialized variables. However, I can only see the variables from the base class but not the ones from the inherited ones. I believe this is due to the fact that when serialized all the child classes get turned into the base class. If this should be right, how can I solve this problem ?
I'm also wondering whether there is a way to get to a solution if I was able to serialize interfaces as all the child classes could inherit from that interface and the problem would be solved. But since I don't have the money to buy odin Inspector nor ANY idea on how to implmement custom serialization that seems to be a dead end, too.
.
Well, at least for me. I hope that you are a generous genius that is willing to help ^_^
.
Ps: I hope that does make some sense... (if it doesn't I would glad to specify myself further)
[Serializable]
public class Condition
{
[SerializeField] int intone;
//is visible
}
public class ConditionChild : Condition
{
[SerializeField] int inttwo;
//is not visible
}
[Serializable]
public class Transition
{
public Transition() { }
[SerializeField] public List<Condition> conditions = new List<Condition>();
}
Answer by Skrimel · Jun 30, 2020 at 12:25 PM
Use SerializeReferenceAttribute for you list - it supports polymorphism of type of serialized elements. Docs for attribute here: https://docs.unity3d.com/ScriptReference/SerializeReference.html Read attentively all serialization restrictions in docs. Supported from Unity 2019.3, as I remember
Your answer
![](https://koobas.hobune.stream/wayback/20220612233123im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Define attacks to choose from 1 Answer
Suggestions for planning project tasks and organizing classes/functionality? 0 Answers
Can't instantiate new class C# 0 Answers
What is the real purpose of ISerializationCallbackReceiver interface? 1 Answer
Argument Exception: J SON must represent an object type. Serialization list of objects 0 Answers