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.
  2. Dismiss Notice

Broken references for prefabs with SerializeReference fields

Discussion in 'Prefabs' started by Ghat-Smith, Feb 13, 2020.

  1. Ghat-Smith

    Ghat-Smith

    Joined:
    Aug 16, 2016
    Posts:
    48
    Hi everybody.

    I'm trying to get advantage of the new SerializeReference attribute, but encounter difficulties to make it work with prefabs.

    Setup :
    • Unity version : 2019.3.0f6
    • A MonoBehaviour script containing a list of C# class (with [SerializeReference] attribute)
    • The script is set in a prefab. And the list contains a class which reference a child GameObject of the prefab itself.
    Problem 1 : Object Reference Support

    When adding the GameObject to the scene, the referencing work as expected : the C# class is reference the child GameObject of the instance itself.

    But after reloading the scene, it is now referencing the child GameObject of the prefab asset.
    I hope the answer won't be something like "SerializeReference is not supporting Unity Object fields" as it would drastically limit its usage.

    EDIT : after trying to reproduce the problem in an empty project, it appears to be worse than I thought. You just need to use SerializeReference attribute on a field, and all Object references of prefab instance can reference prefab asset instead of corresponding instance object.

    Code (CSharp):
    1. [System.Serializable]
    2. public class CorruptingClass
    3. {
    4. }
    5.  
    6. public class TestScript : MonoBehaviour
    7. {
    8.     [SerializeField] private GameObject gameObjectField = null;
    9.     [SerializeReference] private CorruptingClass corruptingField = new CorruptingClass();
    10. }
    11.  
    1. Create a prefab.
    2. Add TestScript on it.
    3. Add a child GameObject and make the gameObjectField referencing the child GameObject.
    4. Save the prefab.
    5. Add the prefab multiple times to a scene (bug not happens if the scene contains only one instance o_O)
    6. Save and reload the scene.
    Expected Result : GameObject fields of prefab instances should reference the child GameObject of the prefab instance.
    Actual Result : GameObject fields of prefab instances are referencing the child GameObject of the prefab asset :(.

    I sent a bug report (Case 1219332).​


    Problem 2 : Overriding Support

    A similar problem has already been discussed a bit on the forum :
    https://forum.unity.com/threads/can...ereference-fields-in-prefab-instances.776669/
    And a bug report has been done :https://issuetracker.unity3d.com/is...erializedreference-fields-in-prefab-instances

    However, I would like to point that, it's not even possible to override the list size. If I try to add an element to my list in the prefab instance, the list will be resized to the prefab original size when reloading the scene.

    Even if it's not supported yet, is there a way to completely ignore a field for prefab logic ?
    I tried to override prefab PropertyModification manually, but without success (I may try again to be sure it's not working).

    Additional note to Unity team : in the topic linked above, you said that documentation should be updated to mention the overriding limitation. Unfortunately this has not been done. Please do it. I wasn't really happy to discover that by myself and I probably won't be the only one.


    I'm going to do additional tests in a new empty project before sending bug reports.
    Just wanted to launch the discussion on the forum at the same time. Maybe I'm doing something wrong, or maybe someone will be able to give some answers.
     
    Last edited: Feb 13, 2020
  2. alex-abreu-unity

    alex-abreu-unity

    Unity Technologies

    Joined:
    Jan 18, 2018
    Posts:
    6
    Hi,

    thank you for your detailed report,

    So indeed, for "Problem 1" and "Problem 2", we are *actively* working on a fix and are in the process of testing and validating it. I can confirm (I just did the tests) that both your issues/cases are currently covered and fixed by it.

    The fix will land as part of 2020.1 and be backported to 2019.3.
     
  3. Ghat-Smith

    Ghat-Smith

    Joined:
    Aug 16, 2016
    Posts:
    48
    Cool ! Thank you for the reply. I'm looking forward to the update :).
     
  4. Tykev

    Tykev

    Joined:
    Aug 25, 2014
    Posts:
    21
    @alex-abreu-unity Can you give us some ETA? For others - the problem 1 still persist in 2019.3.3f1
     
  5. numbercrunchee

    numbercrunchee

    Joined:
    Feb 12, 2018
    Posts:
    4
    Any news on this? This is really a showstopper for our save system. We're working with version 2019.3.x.
     
  6. alex-abreu-unity

    alex-abreu-unity

    Unity Technologies

    Joined:
    Jan 18, 2018
    Posts:
    6
    Hi,
    The fix will be out asap, we are in the process of QA'ing it at the moment. The branch is stable, so I expect it to be out soonish, and backported as soon as things land in our main. It should all be in the next following weeks,

    I will keep you posted on that,
     
    Ghat-Smith likes this.
  7. alex-abreu-unity

    alex-abreu-unity

    Unity Technologies

    Joined:
    Jan 18, 2018
    Posts:
    6
    A small update, the fix has landed in our development branch (for 2020.2) and the backport landed for 2020.1. It should be a few extra weeks for the 2019.3 fix,
     
    Ghat-Smith likes this.
  8. alex-abreu-unity

    alex-abreu-unity

    Unity Technologies

    Joined:
    Jan 18, 2018
    Posts:
    6
    A final update: the fix landed in 2019.3 and will be available in the next 2019.3 release (2019.3.7).
     
    phobos2077 and Ghat-Smith like this.
  9. Tykev

    Tykev

    Joined:
    Aug 25, 2014
    Posts:
    21
    Great, thanks! :)
     
  10. Shrubokrant

    Shrubokrant

    Joined:
    Oct 2, 2016
    Posts:
    80
    Hi,
    Sorry to revive an old thread, but there is again a bug with SerializeReference and prefabs in 2022.2.1f
    (I copied my message from another thread here too because it's relevant)
    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: View attachment 1178568
    On the image, the blue one is correct, the red ones are wrong.

    Is that the intended way, or is that a bug?
     
    Ghat-Smith and UGameStudio like this.
  11. UGameStudio

    UGameStudio

    Joined:
    May 4, 2019
    Posts:
    5
    I have the same problem in 2022.2.0f1
     
  12. Mads-Nyholm

    Mads-Nyholm

    Unity Technologies

    Joined:
    Aug 19, 2013
    Posts:
    215
    This issue is fixed in 2022.2.3f1 which is released soon
     
  13. Blazerker

    Blazerker

    Joined:
    Oct 19, 2012
    Posts:
    5
    Issue still persists in 2022.3.4f1
     
  14. Peter_Olsted

    Peter_Olsted

    Unity Technologies

    Joined:
    Apr 19, 2021
    Posts:
    74