Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Discussion SerializeReference's code maintenance

Discussion in 'Scripting' started by FVS, Nov 28, 2022.

  1. FVS

    FVS

    Joined:
    Aug 10, 2015
    Posts:
    49
    Hello,

    In my current project, SerializeReference attribute is an important feature, it's used to expose properties of different kinds of a character, and I can easily modify each property in the inspector.

    But recently, I've just realized that the feature is very fragile, hence so hard sometimes to maintain the code. There are 2 main issues I've had so far:

    1. When I change the name of the serialized (non-Mono) class, then the item which is an instance of that class will disappear, and I have no idea to restore it rather than re-add it manually.

    2. When I add a new serialized field to the serialized class, all the referenced instances of that class will disappear as well.

    (In both cases, there are red error messages in the console, I don't remember exactly but they were kind of "class not found" and "a serialized property missing" (???).

    So, I open this topic in hope that other dev will share their use-cases of SerializeReference, how to make it maintainable. And specifically, to see if anyone can solve the issues I mentioned above, because it's very likely I'll have to perform the same things again when I do code-refactoring.

    Thank you,
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    4,476
    With respect to this, there is an undocumented attribute called:
    UnityEngine.Scripting.APIUpdating.MovedFromAttribute
    that solves this problem.

    I've never experienced this. What version of Unity are you on? I believe in 2021 the stability of SerializeReference got improved a good amount.
     
    FVS likes this.
  3. FVS

    FVS

    Joined:
    Aug 10, 2015
    Posts:
    49
    Thank you so much for this. Too bad it is undocumented, had to figure out how the syntax should be, but it worked!

    It's weird that I couldn't reproduce the error. I will update this thread if it ever occurs again.
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    4,476
    After some thinking it might've been when you have a type with no serialised fields, and then introduce one later, does it have issues. Not sure if that was resolved; but I'll test later.
     
  5. FVS

    FVS

    Joined:
    Aug 10, 2015
    Posts:
    49
    That's how I had the issue, but maybe there's more to the condition, because I couldn't reproduce on a mockup script, but could reproduce on one of my working class, when I tried to add a new serialized field to a type which initially had none, and the error appear, now I can copy it: "The serialized array of [SerializeReference] objects is missing entry for Refid -2"
     
  6. FVS

    FVS

    Joined:
    Aug 10, 2015
    Posts:
    49
    Oh, and I forgot to mention I'm using Unity 2022.1.23
     
  7. FVS

    FVS

    Joined:
    Aug 10, 2015
    Posts:
    49
    Well, actually the mockup script does have the issue, but not very consistent. The errors only showed once (when I load the project with the scene containing the mockup objects or the first time I Play the scene). In addition to the message above there are more:
    Assertion failed on expression: 'm_ReferenceIndexMap->size() == refMap.size()'
    ssertion failed on expression: 'refMap.size() == m_WriteObjectAccumulator.size()'
    Assertion failed on expression: 'objCnt == refs.size() || (objCnt == refs.size() - 1)'