Search Unity

How can I find the prefab asset a child object is belong to ? I want to destroy a child in a prefab

Discussion in 'Editor & General Support' started by Chocolade, Feb 15, 2020.

  1. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    933
    I want to destroy a gameobject that is a child inside a prefab.
    But there are also some other prefabs above the child object and then the top prefab asset.

    To destroy I need to open the prefab destroy and then close and save the prefab again.

    Code (csharp):
    1.  
    2. if (UnityEditor.PrefabUtility.IsPartOfPrefabInstance(transform))
    3.  
    4.    UnityEditor.PrefabUtility.UnpackPrefabInstance(gameObject,
    5.         UnityEditor.PrefabUnpackMode.Completely,
    6.         UnityEditor.InteractionMode.AutomatedAction);
    7.  
    But instead gameObject how do I know what is the prefab asset ? How do I get the prefab asset if I don't know it's name ?

    For example let's say this is my Hierarchy :

    PrefabA
    GameObjectA
    PrefabB
    GameObjectB

    I want to destroy GameObjectB using DestroyImmediate via code script.
    The problem is that in the editor I did already Overrides > Apply Changes on PrefabA
    So now GameObjectB is part of the PrefabA and I need by code to open PrefabA DestroyImmediate(GameObjectB) then save the changes to PrefabA

    How can I do it ?

    It's like doing it in the editor open the PrefabA delete GameObjectB and close the PrefabA
    I need ot do it by code in a script in a editor script.