Search Unity

Programmatically destroy GameObjects in Prefabs.

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

  1. Joshua_strawchildgames

    Joshua_strawchildgames

    Joined:
    May 27, 2017
    Posts:
    60
    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

    XRA

    Joined:
    Aug 26, 2010
    Posts:
    265
    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):
    1.  
    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);
    11.  
    12.  
     
    Last edited: Dec 3, 2018
    Avalin and michailmoiropoulos like this.
  3. Joshua_strawchildgames

    Joshua_strawchildgames

    Joined:
    May 27, 2017
    Posts:
    60
    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

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    639
    Hi,

    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.
     
    ribbanya and dodoz13 like this.
  5. ImperialDynamics

    ImperialDynamics

    Joined:
    Jun 21, 2018
    Posts:
    21
    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.