Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We've updated our Terms of Service. Please read our blog post from Unity CTO and Co-Founder Joachim Ante here
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

Destroying a GameObject inside a Prefab instance is not allowed

Discussion in 'Prefabs' started by stephero, Sep 15, 2018.

  1. stephero

    stephero

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

    I encounter a very annoying issue with the new prefab workflow.

    I have a "master" GameObject which procedurally generates a sub GameObject as child.
    The child GameObject is not serialized since it's procedurally recomputed from the "Start" callback (both in editor and runtime).
    Note that the child GameObject is flagged as NotEditable AND DontSave via the HideFlags.

    When I duplicate my master GameObject in the editor, or when I instantiate a prefab storing my master GameObject, the sub GameObject is also duplicated. To fix this issue, I used to destroy the "old" sub GameObject. Basically, from the sub GameObject, I store a reference on its master. If this reference is broken, I know that this sub GameObject is duplicated or instantiated, so I could safely destroy it using DestroyImmediate.

    This trick used to work very very well... until the new prefab workflow introduced in 2018.3.0b1.
    In the beta, when I drop a prefab storing a "master" GameObject, I got this very annoying error message:
    InvalidOperationException: Destroying a GameObject inside a Prefab instance is not allowed.
    and my "old" sub GameObject is not properly destroyed.

    Why is this not allowed anymore? How are we supposed to deal with procedurally objects inside prefabs?

    Thanks
     
  2. TooManySugar

    TooManySugar

    Joined:
    Aug 2, 2015
    Posts:
    858
    I've not tested this yet cause my prefabs are broken but this sounds
    "Destroying a GameObject inside a Prefab instance is not allowed."
    This is an operation I do constantly in my tank editor hope this is adressed soon.
     
  3. Mads-Nyholm

    Mads-Nyholm

    Unity Technologies

    Joined:
    Aug 19, 2013
    Posts:
    81
    Hi stephero,
    Please create a bug report with a small repro project, then we will take a look at your use case. Thanks
     
  4. stephero

    stephero

    Joined:
    Feb 8, 2016
    Posts:
    76
    Hi,
    I filled a bug report (1084109), but I discovered something really interesting. This problem only occurs if the prefab has been created with older Unity version! Mine was created in Unity 5.2.0f3.
    I tried to create the prefab with many different Unity versions, but was able to reproduce the bug only when created with 5.2.
    Here is a repro project. Open it in Unity 2018.3.0b1 (or b2) and drag and drop the prefab in a scene. Alternatively you can run the test runner (I made a unit-test which highlights the problem).
    Thanks
     

    Attached Files:

    Last edited by a moderator: Sep 24, 2018
  5. james7132

    james7132

    Joined:
    Mar 6, 2015
    Posts:
    105
    Does reimporting fix the issue? I seem to be running into the same issue when building asset bundles.
     
  6. stephero

    stephero

    Joined:
    Feb 8, 2016
    Posts:
    76
    No reimporting the prefab doesn't fix the issue.
     
  7. konsic

    konsic

    Joined:
    Oct 19, 2015
    Posts:
    404
    I'm importing asset bundles from Blende. I have to unlink them to drag them in Prefab folder first.
    When I parent some prefab, it seems that it looses location information.

    I'm confused with new prefabs and how it works with parenting.


    When imporiting form 2018.2.8 HDRP in 2018.3 HDRP, Unity deletes all folders and scripts. It only leaves sample scene.
     
  8. LeonhardP

    LeonhardP

    Unity Technologies

    Joined:
    Jul 4, 2016
    Posts:
    1,180
    Could you please submit a bug report with a reproduction project? That would be very helpful.
     
  9. MatthieuPr

    MatthieuPr

    Joined:
    May 4, 2017
    Posts:
    53
    I tried to make a small repo to reproduce the bug, sadly the editor keeps going into infinite loop before able to reproduce in separate project. I did submit the infinite loop bug and got reply it was sent do the DEVS, so that is one that will be killed at least...

    On a different note: I was able to solve that error in editor. The cause was editor script dynamically adding and removing nested prefabs and if the prefab was saved with already nested prefabs present in it trying to remove them with an editor script gives that error.

    On a separate note, I am now able to generate that error at RUNTIME in our project, which is very strange cause runtime should not have any prefabs at all. I have not been able to reproduce this part in a separate project, this will most likely be work for today, trying to break nested prefabs some more ;)
     
  10. LeonhardP

    LeonhardP

    Unity Technologies

    Joined:
    Jul 4, 2016
    Posts:
    1,180
    Thanks a lot Matthieu, this is all incredibly helpful!
     
  11. MatthieuPr

    MatthieuPr

    Joined:
    May 4, 2017
    Posts:
    53
    no problem, I spent about 2 days on the new nested prefab system and reported 3 different issues related to our current project. One of them is this specific issue. The next few days will try to do same to the ECS.
     
    LeonhardP likes this.
  12. LeonhardP

    LeonhardP

    Unity Technologies

    Joined:
    Jul 4, 2016
    Posts:
    1,180
  13. konsic

    konsic

    Joined:
    Oct 19, 2015
    Posts:
    404
  14. LeonhardP

    LeonhardP

    Unity Technologies

    Joined:
    Jul 4, 2016
    Posts:
    1,180
    No it's still being worked on. You can follow the resolution status through the issue tracker link I posted above.
     
  15. konsic

    konsic

    Joined:
    Oct 19, 2015
    Posts:
    404
    @LeonhardP it says now
    What does this mean improved prefabs ?

    How can I make sure prefabs are not improved ?
     
  16. LeonhardP

    LeonhardP

    Unity Technologies

    Joined:
    Jul 4, 2016
    Posts:
    1,180
  17. konsic

    konsic

    Joined:
    Oct 19, 2015
    Posts:
    404
    Ok, please fix this in next beta.
     
  18. MatthieuPr

    MatthieuPr

    Joined:
    May 4, 2017
    Posts:
    53
    This is related to the infinite loop bug I reported in here:

    We have fixed this problem and it should not appear in the 2019.1.0a9 version.
    If you are still able to reproduce it on the latest version of Unity, please respond to this email.

    https://issuetracker.unity3d.com/is...hild-prefab-which-has-been-previously-deleted

    Related to the bug for those wandering, I have found the reason and so far I know that is actually works as intended.
    When you are adding GameObject to a prefab in prefab mode, you will get this error when you try to delete that gameObject in editor mode. GameObjects (and I assume nested prefabs as well) can only be added/removed in the sceneMode where they were added and from what I understood, you should only do this in PrefabMode as that is where you edit a prefab, in editor you are working with an INSTANCE of the prefab and not the actual prefab (so can't apply changes etc.)

    In short, old workflows can be throws out of the window cause 80% of them no longer apply in the new nested prefab system. Based on all the tools we have for generating UI dynamically in editor, we can throw all of that away + redo entire UI from scratch to be able to upgrade to Unity 2018.3...
     
    konsic likes this.
  19. mkgame

    mkgame

    Joined:
    Feb 24, 2014
    Posts:
    570
    I'm trying to destroy a GameObject inside of a prefab in editor script for 3DMesh synchronization purposes (Asset: PrefabUpdater): "GameObject.DestroyImmediate (filter.gameObject);". Are there another possibilities in editor script to remove a GameObject in an instantiated prefab. PrefabUtility is able to remove a component, but not a GameObject.

    Should this work in the future releases, in 2019.1.0a9?
     
    Last edited: Dec 31, 2018
  20. NibbleByte3

    NibbleByte3

    Joined:
    Aug 9, 2017
    Posts:
    8
    I believe that you're supposed to use PrefabUtility.LoadPrefabContents() + PrefabUtility.SaveAsPrefabAsset() + PrefabUtility.UnloadPrefabContents() to load prefab instance into memory, modify it and then save it back.
    Read carefully the description of these methods. It seems to work with my tools.

    In the previous workflow you again had to instantiate prefab into your current scene, modify it and apply it back, so I guess this won't change much things. It is now even better, because it doesn't actually instantiate the prefab into your current working scene, which was weird. PrefabUtility.LoadPrefabContents() seems to load the prefab in a scene somewhere in the background and doesn't pop up in the Hierarchy, so be careful not to leak stuff.
     
    Last edited: Jan 9, 2019
    mkgame likes this.
  21. mkgame

    mkgame

    Joined:
    Feb 24, 2014
    Posts:
    570
    I missed this method PrefabUtility.LoadPrefabContents(), could have a better name. I used instead unpack, then I replaced the original prefab. You may right, if this works fine, then it is a more cleaner workflow. Thanks!
     
  22. TooManySugar

    TooManySugar

    Joined:
    Aug 2, 2015
    Posts:
    858
    this is driving me mad. Seriously add the option for this to be possible without entering prefab mode. This prefab system feels designed by a double agent from Unreal engine or something like that to ****up Unity's user base.
     
  23. TooManySugar

    TooManySugar

    Joined:
    Aug 2, 2015
    Posts:
    858
    also, when deleting, it forces you to prefab mode, and if your prefab is very complex with a long tree, like a complex UI it LAAAGS, when deleting objects. Plus any change you make is pretty much permanent... WT*************