Destroying a Prefab inside a Prefab (Placed on a Canvas)

Hi,

I,ve tried for 3 hours now to try to work out the following…

I have a Unit Gameobject which has a Prefab of HPBar (The bar is drawn on the only canvas in overlay mode)

I can destroy the unit but the HPBar remains once the unit has been destroyed.

I have tried numerous things but I keep getting

Destroying assets is not permitted to avoid data loss.
If you really want to remove an asset use DestroyImmediate (theObject, true);
UnityEngine.Object:smile:estroy(Object)
DestroyHP:smile:estroyHPBar() (at Assets/Scenes/Prefabs/DestroyHP.cs:11)
Agent:Update() (at Assets/Scenes/Agent.cs:58)

I initially thought that when you destroyed an object it automatically deleted any prefabs that were attached to it but it seems not.

I have 4 units created which at run time their respective HP bars are placed and parented with the Canvas element.

How do I tell the code to the Destroyed Gamebject?

if(UnitHP <= 0f)
{
//Create an explosion at the location
GameObject explosion = Instantiate(DeathAnim,transform.position,Quaternion.identity);

//Destroy the explosion prefab after 5 seconds.
Destroy(explosion,5);

Canvas getCanvas = FindObjectOfType<Canvas>();
/*Now how do I find the
attached prefab which is a child of Canvas to destroy it from the hierarchy.*/


//Destroy the actual gameobject this script is attached to.
Destroy(gameObject);
}

All 4 Units have a HPBar Prefab called HPPrefab so how do I know which is the correct one to destroy in the hierarchy.

Sorry for the long post but I tried to give as much info as poss.

Many thanks for any replies!

When you get this error:

“Destroying assets is not permitted to avoid data loss.”

This means you are attempting to destroy the reference to the Prefab on disk, NOT the instance of it in your scene.

So if at the top you have a public GameObject like so:

public GameObject myPrefab;

You cannot destroy that with Destroy(); That’s the warning you’re seeing.

What you probably want is to destroy the instance of that prefab that you create with Instantiate.

Sorry for delayed response I was not well over the weekend.

From what I gather is where you say I have a public GameObject HPBar I should just use Destroy(HPBar)??

this is what I have at the moment.

   //Health Representation
    public GameObject HPPrefab;
    //Access to the Health bar itself.
    public Image HPBar;
    //Access to the HPBarfill.
    public Image HpBarFilled;
    //HPbar positioning
    public float HpOffset = 0.1f;



   if(UnitHP <= 0f)
        {  
            //Create an explosion at the location
            GameObject explosion = Instantiate(DeathAnim,transform.position,Quaternion.identity);

            //Destroy the explosion prefab after 5 seconds.
            Destroy(explosion,5);

             Canvas getCanvas = FindObjectOfType<Canvas>();
             /*Now how do I find the
             attached prefab which is a child of Canvas to destroy it from the hierarchy.*/
            
           
            //Destroy the actual gameobject this script is attached to.
            Destroy(gameObject);
        }

So as you can see I am trying to destroy the main GameObject + any Associated Prefabs with it.

Or whats the best way to approach destroying a prefab inside a prefab, I feel its something easy that I am missing.

Many thnaks for the reply!

When you drag a reference from the Project folder, that is an asset on disk.

You cannot destroy that using Destroy(). That’s the error you’re seeing.

What you CAN Destroy() is the copy of it that you Instantiate, and that is what you want to Destroy() I presume.

Ahhh I think I understand now

 void Start()
    {
        GetComponent<NavMeshAgent>();
      

        StartCoroutine(Move());
        Deselect();

        HPBar = Instantiate(HPPrefab, FindObjectOfType<Canvas>().transform).GetComponent<Image>();
        HpBarFilled = new List<Image>(HPBar.GetComponentsInChildren<Image>()).Find(img => img != HPBar);


    }

So I should call Destroy(HPBar); Is that correct?

When I do this is does destroy the HPBar as expected but it does not remove it from the Canvas,s Hierarachy? :S

Assuming line 9 compiles without error, then I deduce that HPBar above is a UnityEngine.UI.Image, which is only a component. If you destroy that, the underlying GameObject and any other components on it will remain.

If you want to destroy the entire GameObject that the HPBar Image is attached to, including everything below it, then use:

Destroy( HPBar.gameObject);

Omg, I am so stupid…

Not sure why I did not think of that, Sometimes can’t see the wood for the tree’s lol

Thanks so much for that answer!!

You’re welcome! Unity is built entirely on the concept of a hierarchy of GameObjects in a scene, and each GameObject differs ONLY in what components it has. Every GameObject has a Transform component, but after that every other component is optional. When you put it into your brain like that it can help you get to the bottom of what is actually happening. In the case above, that HPBar points at a Component, and Components always have a handy link to the GameObject they’re on, shortcutted as .gameObject, as well as a handy shortcut to the Transform, as .transform.