Inherited types are not saved and revert back to lowest type.

Issue #13 resolved
Joris Huijbregts created an issue

I've written the following code, which allows me to create new objects of anything that inherits from Zone, as expected ( http://i.imgur.com/chXSbLV.png ).

However, when reloading the scene all serialized data is lost and all types revert back to Zone. The serialized data of Zone (zoneID) persists, but all additional data is lost.

public class TestScript : SerializedMonoBehaviour { public Zone[] zones; }

[System.Serializable] public class Zone { public int zoneID;

}

[System.Serializable] public class SpawnZone : Zone { public Vector3 size; }

[System.Serializable] public class GoalZone : Zone { public bool someGoalVar; }

[System.Serializable] public class SpecificGoalZone : GoalZone { public float a; }

[System.Serializable] public class AnotherSpecificGoalZone : GoalZone { public int a; }

Comments (4)

  1. Tor Esa Vestergaard
    • changed status to open

    It does work, in fact - this is a case of currently bad UX more than anything else. Your type is marked serializable, so Unity will actually serialize it (badly), and as a policy we avoid serializing any member that Unity serializes. If we didn't do this, we would usually serialize far more data than necessary and there would be a lot of data duplication between us and Unity.

    A solution to the issue would be to mark it as private and use [OdinSerialize] instead - which will cause Odin to serialize it. Or not mark the class as [Serializable], which will also make Unity ignore it, and thus cause Odin to serialize it.

    It's fairly trivial to change this behaviour, but we've discussed wanting a different solution instead - such as showing in the inspector which property has which serialization backing. We haven't figured out a nice solution for that yet, though. We agree that it's confusing, though, and something definitely needs to change.

    We would appreciate any suggestions for which approach may be best to resolve this issue.

    Edit: It is a bug that you could even use polymorphism for a Unity-backed list, though - a fix for this will be included in the next patch.

  2. Log in to comment