Destroying assets is not permitted to avoid data loss. (878961)

I finished creating the particles, I configured it so that if the enemy dies the particles appear and then these particles will be destroyed. But when it is going to be destroyed the following error appears:

Destroying assets is not permitted to avoid data loss.
If you really want to remove an asset use DestroyImmediate(theObject, true);
UnityEngine.Object: Destroy (UnityEngine.Object)
Ammo2:OnTriggerEnter2D (UnityEngine.Collider2D) (at Assets/Scripts/Ammo2.cs:26)

What could I do to fix this error?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Munição2 : MonoBehaviour
{
    public ParticleSystem Explosao_Tiro1;

    void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {
       
    }

    void OnTriggerEnter2D(Collider2D outro)
    {
        if(outro.gameObject.CompareTag("Parede"))
        {  
            Instantiate(Explosao_Tiro1, transform.position, Quaternion.identity);
            Destroy(gameObject);
            Destroy(Explosao_Tiro1);
        }
    }
}

You can’t destroy the prefab. That’s what you’re doing.

If you save what Instantiate returns, you may destroy that.

In the above code, you are not using what Instantiate() returns

7 Likes

Bro, can you give me an example of how to use this Return please? :frowning: I tried to search on how to use this Return, but it didn’t help much. I’ve been trying to fix this error for hours. ;-;

original: the prefab on disk

copy: the copy of it that was intantiated

Destroy with second argument: destroy it in 2 seconds time

var copy = Instantiate(original);
Destroy(copy, 2.0f);

SUPER-common pattern, used EVERYWHERE in Unity.

8 Likes

Thank you very much. :smile:

2 Likes