Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Should SerializeReference fields be deep-copied in SerializedProperty arrays in the inspector?

Discussion in '2019.3 Beta' started by Xarbrough, Mar 13, 2020.

  1. Xarbrough

    Xarbrough

    Joined:
    Dec 11, 2014
    Posts:
    1,188
    For the past time, arrays in the Unity inspector automatically copied their last entry when a new entry was added. So, when a user increases the size from 2 to 3 elements, both elements 2 and 3 are copied. Previously this meant, that the copy was shallow. If the data contained structs (e.g. an int), the data was deep-copied (two separate instances of the number), but if the data contained references to Unity Objects (ScriptableObject), only the reference to the object was copied, but of course not the object itself.

    Now, if I have a SerializeReference in an array and increase its size, the last element is also a shallow copy. But in this case, this means that elements 2 and 3 are identical and point to the same instance of the object. I assume that this is not what we want in the usual case. For example, if we have a list of Animal base types filled with a Cat and a Dog instance, I don't want to put the Dog instance in two slots when resizing. Instead, I want to create a new instance. So would it make sense to let SerializeReference be a deep-copy in arrays when resizing by default?

    I know that I can implement a custom editor to handle this case the way I want, but it's still a question for the Unity default inspector or when using the debug inspector.
     
    Last edited: Apr 6, 2022
  2. Yanfundi

    Yanfundi

    Joined:
    Nov 25, 2018
    Posts:
    7
    yes I agree, I found it the hard way. Is there an/some easy way to do so? (I don't want to create CustomEditor to every single script that uses SerializeReference)