Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Official SerializeReference improvements in Unity 2021 LTS

Discussion in 'Scripting' started by Unity_Joseph, Jun 30, 2022.

  1. yu_yang

    yu_yang

    Joined:
    May 3, 2015
    Posts:
    85
    oscarAbraham and spiney199 like this.
  2. MartinBarrette

    MartinBarrette

    Unity Technologies

    Joined:
    Oct 22, 2019
    Posts:
    26
    The MovedFrom bug https://issuetracker.unity3d.com/issues/movedfrom-attribute-doesnt-work-on-prefab-overrides is actively being worked on currently and hopefully will be resolved in the coming days.

    @yu-yang reported bug: https://issuetracker.unity3d.com/is...working-incorrectly-when-using-prefab-variant is fixed and in the process of being integrated/backported. (Sorry did not see yu-yang, post above when I was replying to you Oscar).
    Hope this helps.
     
    oscarAbraham likes this.
  3. yu_yang

    yu_yang

    Joined:
    May 3, 2015
    Posts:
    85
    How to change SerializeReference object type without managed id changing?

    Code (CSharp):
    1.     class MyMonoBehaviour : MonoBehaviour
    2.     {
    3.         [SerializeReference]
    4.         object myObject;
    5.         [SerializeReference]
    6.         object referenceToMyObject;
    7.     }
    If I change myObject to a new value with different type, referenceToMyObject will lose reference to myObject because the new value of myObject has a different managed id. Is there a way to keep managed id when I change SerializeReference object type?
     
  4. Thomas-Bousquet

    Thomas-Bousquet

    Joined:
    Dec 19, 2016
    Posts:
    41
    Is there a plan to update the UX for SerializeReference in the editor ?

    All the examples from the articles are assigning values from code, but a major use case is for these values to be input in the editor using the inspector.

    However, at this point, if one declares field/array to be set by editor users, they just get empty field, with no control to create an object of a compatible type; even when using the + button.
    upload_2022-11-30_15-35-40.png

    There are a few packages out there that offer attributes to change that ([1] and [2]); but it really feels this should be done natively; since:
    1. without the extension, SerializeReference are unusable by data creators out of the box.
    2. packages can't easily (at all?) customize the +/- of the Collection UI using attributes.
     
    kvfreedom, PanthenEye and JulianNeil like this.
  5. Shrubokrant

    Shrubokrant

    Joined:
    Oct 2, 2016
    Posts:
    80
    (Copying my message here since it seems to be the more active thread)
    Hi,
    Sorry to revive an old thread, but there is again a bug with SerializeReference and prefabs in 2022.2.1f
    EDIT: the bug may be coming from Odin Inspector, I'm not sure.
    EDIT2: Tested with and without Odin, the bug is definitely on Unity's side
    Consider the following scripts:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. namespace BFT
    4. {
    5.     public class RefTestComponent : MonoBehaviour
    6.     {
    7.         [SerializeReference]
    8.         public IRefTest RefTest;
    9.     }
    10. }
    Code (CSharp):
    1. namespace BFT
    2. {
    3.     public interface IRefTest
    4.     {
    5.    
    6.     }
    7. }
    Code (CSharp):
    1. namespace BFT
    2. {
    3.     public interface INestedRefTest
    4.     {
    5.    
    6.     }
    7. }
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. namespace BFT
    4. {
    5.     public class NestedRefTestImplementation :INestedRefTest
    6.     {
    7.         public GameObject go;
    8.         [SerializeReference]
    9.         public INestedRefTest NestedRef;
    10.     }
    11. }
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. namespace BFT
    4. {
    5.     public class RefTestImplementation :IRefTest
    6.     {
    7.         public GameObject go;
    8.         [SerializeReference]
    9.         public INestedRefTest NestedRef;
    10.     }
    11. }
    When putting the following script in a prefab, linking to its own gameobject, then when creating an instance of the prefab, only the first reference (the one in RefTestImplementation) points to itself in the instance, the rest of the fields are pointing to the prefab game object:
    In the image, the blue one is correct, the red ones are wrong.
     
    DeuS, Nivagia, Bunny83 and 2 others like this.