- Home /
Prefab Particle clones won't destroy, C#
This is so simple that I know I'm going to feel like a dunce when someone tells me what's wrong with this.
So I have a prefab of a particle system that plays as the character walks around. (It's water splashing around his feet). I have the following code on a collider and everything works fine; the particles show up as he walks where they're supposed to.
However they're not being destroyed in the hierarchy. So at the end of the level I have a zillion particle clones clogging it up even though they're not visible in the scene. Here's my code:
public ParticleSystem waterSplash;
public GameObject constructor;
void Start () {
}
void Update () {
}
void OnTriggerEnter(Collider other)
{
if(other.gameObject.CompareTag("Player"))
{
print( "collided!");
GameObject newSplash = Instantiate (waterSplash, constructor.transform.position, constructor.transform.rotation) as GameObject;
Destroy(newSplash, 2);
}
}
}
Answer by blueteak · Oct 14, 2013 at 02:50 AM
I'm not sure what is wrong with your code, but what I do to destroy one-shot particle effects is have a script attached to it like this: public float DestroyTime;
void Start()
{
StartCoroutine("DestroyMe");
}
IEnumerator DestroyMe()
{
yield retuern new WaitForSeconds(DestroyTime);
Destroy(gameObject);
}
Perfect! I didn't even think about adding a script to the prefab itself.
Works fine now. Thanks so much!