This is a 2D project, I have a projectile that plays a particle effect on collision the projectile gets destroyed the particle effect plays but then stays in the hierarchy thus remaining in the game and I’m sure will cause issues when they pile up. Not really sure whats going on here or how to destroy the hit effect. Any help would be greatly appreciated =D
public class Projectile : MonoBehaviour
{
public GameObject hitEffect;
private void OnCollisionEnter2D(Collision2D collision)
{
Instantiate(hitEffect, transform.position, Quaternion.identity);
Destroy(gameObject);
}
}
The Instantiate() routine is returning a reference to your new effects.
You can assign that to a temp GameObject and then immediate call Destroy on that new GameObject, but give it a second argument which is the delay before destruction:
var foo = Instantiate(.. etc..);
Destroy( foo, 2.0f);
ahhh thanks you very much, I tried to destroy the hiteffect and it wouldnt allow me, didnt realize i had to make a variable that = Instantiate to destroy it =D thanks for the reply
In the context above, the reason is you probably put a prefab into that hitEffect slot, and a prefab is an actual asset on the disk. That is why Unity says you can’t destroy it.