Unable to serialize interface references in class field

Issue #445 resolved
Joseph Brazil created an issue

I have a class that inherits from SerializedMonoBehaviour. This behaviour has a field (with SerializeField) of a System.Serializable class. This field class has a field (with SerializeField) of an Interface.

    - serialized field (serialized class)
        - serialized field (interface)

The interface field is not serialized, individually or as part of a collection.

The same interface field is serialized when part of the SerializedMonoBehaviour itself.

Changing SerializeField to OdinSerialize does not serialize the field either (reloading the scene clears the references), despite the message about the field not being serialized disappearing.

Unity 2017.4.2f2 Windows 10 x64

Comments (3)

  1. Tor Esa Vestergaard

    This is not a bug, but simply Odin's regular behaviour - serialization of an object is decided at the "root", so to speak, IE, at the SerializedMonoBehaviour level. By default, Odin lets Unity handle everything that Unity would normally serialize, and only serializes things that Unity does not serialize. You can mark your serialized root field with [OdinSerialize] to force the matter (making both Unity and Odin serialize it), or remove [Serializable] from the serialized class, so Unity won't touch it and Odin takes over.

    And of course, always remember to check the serialization debugger!

  2. Joseph Brazil reporter

    Ah, that makes sense. Adding OdinSerialize to the root field seems to solve it. Surprised I didn't try that to begin with. Thanks!

  3. Log in to comment