Search Unity

  1. The 2022.1 beta is now available for testing. To find out what's new, have a look at our 2022.1 beta blog post.
    Dismiss Notice

[SerializeReference] Assets lose its Data when a Class name is changed

Discussion in 'Scripting' started by KMacro, Feb 12, 2020.

  1. KMacro

    KMacro

    Joined:
    Jan 7, 2019
    Posts:
    48
    I am running into the bug detailed by the bug report at the following link:
    [SerializeReference] Assets lose its Data when a Class name is changed
    The bug is marked as fixed in both 2020.1 and 2019.3, but I have managed to reproduce the bug in both 2019.3.0f6 and 2020.1.0a21.2690. The only additional information I see was posted by the user who originally created the bug report saying "Should be fixed in 2019.4".

    What is the current status of this bug fix?

    As a note, my use case that brought me to this bug is slightly different than the bug as reported (though the bug as reported does still exist). I have the following attached to an empty game object in my scene:
    Code (CSharp):
    1. public class ReferenceTest : MonoBehaviour
    2. {
    3.     [SerializeReference] public List<HolderClass> holderClasses;
    4. }
    5.  
    6. [Serializable] public abstract class BaseClass {}
    7. [Serializable] public class Class1 : BaseClass {}
    8. [Serializable] public class Class2 : BaseClass {}
    9. [Serializable] public class Class3 : BaseClass {}
    10. [Serializable]
    11. public class HolderClass
    12. {
    13.     [SerializeReference] public BaseClass baseClass;
    14. }
    and the following Editor code:
    Code (CSharp):
    1. [CustomEditor(typeof(ReferenceTest))]
    2. public class ReferenceTestEditor : Editor
    3. {
    4.     public void OnEnable()
    5.     {
    6.         var script = target as ReferenceTest;
    7.        
    8.         if (script.holderClasses == null)
    9.         {
    10.             script.holderClasses = new List<HolderClass>();
    11.         }
    12.  
    13.         foreach (var da in AppDomain.CurrentDomain.GetAssemblies())
    14.         {
    15.             var assemblyTypes = da.GetTypes().Where(type => type.IsSubclassOf(typeof(BaseClass)) && !type.IsAbstract);
    16.             foreach (var at in assemblyTypes)
    17.             {
    18.                 if (script.holderClasses.All(x => x.baseClass != null && at != x.baseClass.GetType()))
    19.                 {
    20.                     script.holderClasses.Add(new HolderClass() {baseClass = (BaseClass)Activator.CreateInstance(at)});
    21.                 }
    22.             }
    23.  
    24.             for (var i = script.holderClasses.Count - 1; i >= 0; i--)
    25.             {
    26.                 if (script.holderClasses[i].baseClass == null)
    27.                 {
    28.                     script.holderClasses.Remove(script.holderClasses[i]);
    29.                 }
    30.             }
    31.         }
    32.     }
    33.  
    34.     public override void OnInspectorGUI()
    35.     {
    36.         var script = target as ReferenceTest;
    37.  
    38.         foreach (var hc in script.holderClasses)
    39.         {
    40.             EditorGUILayout.LabelField(hc.baseClass.GetType().ToString());
    41.         }
    42.     }
    43. }
    Changing the name of any of the classes derived from BaseClass or removing the class entirely not only produces the error mentioned in the bug report, but makes all entries of the holderClasses list null.

    I have submitted a minimual repro project for both 2019.3 and 2020.1 with case numbers 1218905 and 1218903 respectively.
     
  2. kvfreedom

    kvfreedom

    Joined:
    Mar 30, 2015
    Posts:
    34

    Attached Files:

  3. KMacro

    KMacro

    Joined:
    Jan 7, 2019
    Posts:
    48
    kvfreedom likes this.
  4. Maisey

    Maisey

    Joined:
    Feb 17, 2014
    Posts:
    286
    I hope they backport this eventual fix, it's incredible frustrating. :/
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    21,369
    Some info about Missing script warnings, GUIDs, renaming GUIDs, etc:

    https://forum.unity.com/threads/problem-with-git-and-missing-scripts.1090876/#post-7024801
    https://forum.unity.com/threads/scr...ead-after-loading-editor.998413/#post-6487297
    https://forum.unity.com/threads/scr...ead-after-loading-editor.998413/#post-6488230

    EVERYTHING in Unity is connected to the above GUID. It is super-easy to inadvertently change it by renaming outside of Unity. Don't do that. Instead:

    - close Visual Studio (important!)
    - rename the file(s) in Unity
    - in Unity do Assets -> Open C# Project to reopen Visual Studio
    - now rename the actual classes, and MAKE SURE THE FILE NAMES DO NOT CHANGE!
     
unityunity