Search Unity

  1. Click here to see what's on sale for the "Best of Super Sale" on the Asset Store
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Programmatically destroy GameObjects in Prefabs.

Discussion in 'Prefabs' started by Joshua_strawchildgames, Dec 3, 2018.

  1. Joshua_strawchildgames


    May 27, 2017
    Hey folks, can I get a straight forward explanation or link to some documentation on how to delete child objects in prefabs (for editor code)? I can't find a single thing for this and I can't run any of my utility scripts because this raises an exception and halts the whole utility process.
  2. XRA


    Aug 26, 2010
    wondering the same,
    I tried first Unpacking the prefab instance, but the unpack fails, it does detect that it is a prefab however

    *EDIT* actually this works, not sure if correct, basically just first checks if it is part of a prefab instance and destroys the instance's handle, then does the usual DestroyImmediate either way. This is for cleaning up objects with some scene management editor-only stuff.

    Without the check for prefab instance and destroying the handle I get the error about not being able to delete prefab instances.

    Code (CSharp):
    2. if ( PrefabUtility.IsPartOfPrefabInstance(transform) )
    3. {
    4.     //if a part of a prefab instance then get the instance handle
    5.     Object prefabInstance = PrefabUtility.GetPrefabInstanceHandle(transform);
    6.     //destroy the handle
    7.     GameObject.DestroyImmediate(prefabInstance);
    8. }
    9. //the usual destroy immediate to clean up scene objects
    10. GameObject.DestroyImmediate(transform.gameObject,true);
    Last edited: Dec 3, 2018
    michailmoiropoulos likes this.
  3. Joshua_strawchildgames


    May 27, 2017
    I tried the unpack as well. Basically followed the documentation for PrefabUtility.RemoveAddedGameObject(), which states that it can remove game objects including those in instances, however that failed.

    I'll try this out in a bit though! I got things working by unpacking my prefabs in the scene view, but it's not the best workflow. Thanks for the tip!
  4. SteenLund


    Unity Technologies

    Jan 20, 2011

    Prior to 2018.3 we allowed destroying gameobjects from a prefab instance in the editor. The result was a Disconnected Prefab. The workflow with disconnected prefabs has been removed as it does not work correctly with nesting and variants.

    Depending on your usecase you have different options.
    If it is editor tooling for modifying a prefab and saving it again then use PrefabUtility.LoadPrefabContent.
    If it is editor tooling for simply instantiating a prefab removing some GameObject but never saving again, use PrefabUtility.UnPackPrefab before destroying your objects.

    If it is gameplay related and used during playmode, simply don't use PrefabUtility.InstantiatePrefab but use GameObject.Instantiate instead.
    dodoz13 likes this.
  5. ImperialDynamics


    Jun 21, 2018
    SteenLund, you're the best, i've seen your comments in many fora and you are a really helpful guy, thanks for that.
    My case is the first case (modifying prefab and saving it again). Specifically i'm trying to delete a child of the prefab. It works but for some reason it doesn't work at first try. I have to click the child more than once (3-4 times) then it disappears from the prefab view. Why is that? How can i make it instantaneous? Thank you.