Search Unity

How to prevent a "HideAndDontSave" child object from setting my prefabs as Overridden?

Discussion in 'Prefabs' started by stephero, Jul 25, 2019.

  1. stephero

    stephero

    Joined:
    Feb 8, 2016
    Posts:
    123
    Hi guys,

    I have an GameObject A. Its creates a "procedural" child GameObject B in editor and at runtime. The child GameObject B is not meant to be modified nor saved by the user, so its HideFlags are set HideAndDontSave.

    I want to create a prefab with my GameObject A in it.
    It looks fine in the prefab editor (B is not visible):
    Screenshot_21.png

    But the problem appears when I instantiate my prefab. Each instance of it will show me the "Overrides" menu and tell me that a new child object B has been added, which is very annoying:
    Screenshot_25.png

    I also tried to put all the HideFlags (basically HideFlags.HideAndDontSave | HideFlags.HideInInspector), but it doesn't help.

    Here is the code which create my "B" GameObject from "A":
    Code (CSharp):
    1. [ExecuteInEditMode]
    2. public class A : MonoBehaviour
    3. {
    4.     B m_B = null;
    5.  
    6.     void Awake()
    7.     {
    8.         m_B = (new GameObject("B", typeof(B))).GetComponent<B>();
    9.         m_B.transform.SetParent(transform, false);
    10.  
    11.         m_B.hideFlags = HideFlags.HideAndDontSave;
    12.         m_B.gameObject.hideFlags = HideFlags.HideAndDontSave ;
    13.     }
    14. }
    Do you have any idea to prevents the Prefab system from taking my HideAndDontSave into account?

    Many thanks for your help!
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    This is a known issue - both Text Mesh Pro and Cinemachine have bugs related to this. I'd report it as a bug, seeing as "HideAndDontSave" should actually mean that, and the prefab system should respect it.
     
  3. stephero

    stephero

    Joined:
    Feb 8, 2016
    Posts:
    123
  4. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    It's not quite the same issue. One is added objects appearing as overrides even though they have HideAndDontSave flag set. The other (you linked to) is about an object without HideAndDontSave appearing as having overrides due to referencing (via objects fields) another object which does have HideAndDontSave set.

    In the linked case we'd have to run code for every object reference property in order to address it, which may have some performance implications. In the issue you reported here, we'd only have to do one check for the added object as a whole. The needed fixes are quite different that way, and fixing one will not automatically fix the other as well.
     
  5. stephero

    stephero

    Joined:
    Feb 8, 2016
    Posts:
    123
    You are right. I will report a new bug then. Thanks!
     
    runevision likes this.
  6. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    An update on this.

    We are working on making added GameObjects and added Components with HideAndDontSave (or just DontSave, really, specifically DontSaveInEditor) not show up in the Overrides dropdown because they can't be applied or reverted anyway.

    However, we likely won't address the issue of object fields that refer to HideAndDontSave objects and are marked as overrides for that reason. The reason we can't just hide those in the overrides dropdown too is that while clicking Apply All won't have any effect on these, clicking Revert All actually will. And the overrides dropdown should show all overrides that will have an effect either when applying or reverting.

    Specifically, property overrides block changes made in the Prefab Asset from affecting the Prefab instance. This is also the case when an object field override is pointing to a HideAndDontSave object. And the overrides dropdown should show everything that can cause changes in Prefab Assets to not affect Prefab instances - that's one of its major purposes. Added components and added GameObjects will never block changes made in the Prefab Asset, so that's why they don't have this issue.

    Our recommendation is to make sure that fields you want to point to HideAndDontSave objects should not be serialized at all. Then they will never show up as overrides.
     
    stephero and SugoiDev like this.
  7. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    Do you have a case number for this?
     
  8. stephero

    stephero

    Joined:
    Feb 8, 2016
    Posts:
    123
    Hi,
    Thank you for your update and follow-up in your previous post. I definitely understand the limitations, which will be totally fine in my case, so I would be very happy :)
    About the bug I reported, it's Case 1173465.
    Thanks!
     
    runevision likes this.
  9. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    Update: A fix so HideAndDontSave added GameObjects and components don't show up in the overrides dropdown will be in 2019.3 (from alpha 13). We're also trying to get it backported to 2018.4 and 2019.2.
     
    viesc123, CDF, SugoiDev and 1 other person like this.
  10. stephero

    stephero

    Joined:
    Feb 8, 2016
    Posts:
    123
    Thank you very much for the fix and for the support, can't wait to give it a try!
     
  11. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    1,311
    I need this so bad right now.