Search Unity

Reserialization (FormerlySerializedAs) a field on MonoBehaviour in a namespace fails on prefab varia

Discussion in 'Prefabs' started by ARGB32, Sep 13, 2019.

  1. ARGB32

    ARGB32

    Joined:
    Feb 20, 2013
    Posts:
    15
    I've posted a bug with as simple as example as I could (Case 1183879) - posting the issue here in case others have struggled with it or have a solution.

    When attempting to reserialize (FormerlySerializedAs) a field on a monobehaviour that's defined inside a namespace any prefab variants that have that MonoBehaviour will fail to apply any serialized variant modifications - reverts to prefab source values

    Effectively this means any namespace based MonoBehaviour on a prefab variant cannot change any serialized field names without losing the fields variant values invalidating the whole point of the variant.

    Code (CSharp):
    1.  
    2. [Serializable]
    3. public class TestObject
    4. {
    5.     public bool ForceReserialize;
    6.     public bool MyValue;
    7. }
    8. namespace MyNamespace
    9. {
    10.     // fails on prefab variants
    11.     public class TestClassInNamespace : MonoBehaviour
    12.     {
    13. #if !FORMERLY_SERIALIZED_AS
    14.         public TestObject TestObject;
    15. #else
    16.         [FormerlySerializedAs("TestObject")]
    17.         public TestObject TestObjectNewName;
    18. #endif
    19.     }
    20. }
    Code (CSharp):
    1.  
    2. [Serializable]
    3. public class TestObject
    4. {
    5.     public bool ForceReserialize;
    6.     public bool MyValue;
    7. }
    8. public class TestClass : MonoBehaviour
    9. {
    10. #if !FORMERLY_SERIALIZED_AS
    11.     public TestObject TestObject;
    12. #else
    13.     [FormerlySerializedAs("TestObject")]
    14.     public TestObject TestObjectNewName;
    15. #endif
    16. }
     
    nuverian likes this.
  2. ARGB32

    ARGB32

    Joined:
    Feb 20, 2013
    Posts:
    15
    nuverian likes this.