Getting Error when destroying DeathVFX

I am using Unity 2019.2.9f1 version and when I am going to destroy DeathVFX it is giving me error.

Destroying assets is not permitted to avoid data loss.
If you really want to remove an asset use DestroyImmediate (theObject, true);
UnityEngine.Object:Destroy(Object, Single)

Can you please help me to fix this isse.

Thanks

Hi Pratik,

Check which object your code is trying to destroy. It mustn’t destroy the prefab but the instantiated object. Remember you can also look at the lecture code changes via the link in the Resources of each lecture.

using UnityEngine;

public class Health : MonoBehaviour
{
    [SerializeField] float health = 100f;
    [SerializeField] GameObject deathVFX;

    public void DealDamage(float damage)
    {
        health -= damage;

        if (health <= 0)
        {
            TriggerDeathVFX();
            Destroy(gameObject);
        }
    }

    private void TriggerDeathVFX()
    {
        if (!deathVFX) { return;  }
        Instantiate(deathVFX, transform.position, transform.rotation);
        Destroy(deathVFX, 1f);
    }
}

This is my code. and I am getting error at last line
Destroy(deathVFX, 1f);

Can you please help me to fix this issue.

Thanks

deathVFX references the prefab which you instantiate with this line:

 Instantiate(deathVFX, transform.position, transform.rotation);

With this line, you try to destroy the prefab:

Destroy(deathVFX, 1f);

What do you want to destroy? The prefab or the instantiated object?

Have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture?

Thanks Nina,

I understand the issue. I am trying to delete Prefab instead of Instantiated object.

Thanks

You’re welcome. :slight_smile:
Were you able to fix the issue?

Hint: All you need is to assign the instantiated object to a variable and pass on the variable to the Destroy method.

Yes I have fixed issue.

Thanks

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms