Search Unity

C# serialization with polymorphism/inheritance.

Discussion in 'Scripting' started by McGravity, Dec 5, 2015.

  1. McGravity

    McGravity

    Joined:
    Nov 2, 2013
    Posts:
    43
    Hello,

    on the one hand, I read a couple of times that a class derived from ScriptableObject can not be serialized. And indeed I experience the Exception myself which gets thrown when trying to serialize a SO.
    Code (CSharp):
    1.  
    2. SerializationException: Type ScriptableObject in assembly Assembly-CSharp, Version=0.0.0.0,Culture=neutral, PublicKeyToken=null is not marked as serializable.
    On the other hand I always read that inheritance/polymorphism can only be handled correctly on serialization when deriving from SO. I'm confused.

    I don't get how you have to build the class structure in order to serialize objects which use inheritance. Atm my structure is like this:

    Code (CSharp):
    1.  
    2.     public class Model : ScriptableObject{
    3.     }
    4.  
    5.     [Serializable]
    6.     public class CreatureModel : Model{
    7.         // A couple of primitive types to serialize.
    8.     }
    9.  
    10.     [Serializable]
    11.     public class CitizenModel : CreatureModel{
    12.         // A couple of primitive types to serialize.
    13.     }
    This of course throws an exception that Model isn't marked as serializable. Marking it as serializable will throw the excpetion from above. There is something I don't get about this, what is it? :)
     
    Last edited: Dec 5, 2015
  2. Deleted User

    Deleted User

    Guest

    I don't think MonoBehaviours and ScriptableObject do not need the Serializable attribute to be serialized but try adding the attribute on top of your 'Model' class and that should fix it.
     
  3. McGravity

    McGravity

    Joined:
    Nov 2, 2013
    Posts:
    43
    That will throw an exception saying that ScriptableObject is not marked as serializable.
     
  4. Deleted User

    Deleted User

    Guest

    Did you try adding the Serializable attribute to the Model class?

    This is what I have:

    Code (CSharp):
    1. public class Test : ScriptableObject {}
    2.  
    3. [System.Serializable]
    4. public class Test2 : Test{}
    5.  
    No errors. There has to be something you're trying to do that is generating the error. You said it's generated when trying to serialize the object. Are you trying to serialize it yourself?
     
  5. McGravity

    McGravity

    Joined:
    Nov 2, 2013
    Posts:
    43
    Yes, I'm creating a FileStream and serialize an (model) object via the serialize method of a BinaryFormatter.

    Nevertheless, I just tried to remove the SO inheritance, serialized an object of Type CitizenModel and deserialized it with no problem as it being a CreatureModel for example. So maybe the problem that objects of type subclass being of type superclass after deserialization doesn't apply to my case.

    It seems like I mixed up Unity serialization and C# serialization in my head.
     
    Last edited: Dec 5, 2015
  6. Deleted User

    Deleted User

    Guest

    Oh I see.

    What I've seen other people do is check to see if the class is a Unity type before serializing it leaving Unity to serialize its own types and you to serialize the ones Unity can't.
     
unityunity