I acquired this snippit of code and it instantiates a particle system prefab. The problem I'm having is the clones do not get destroyed after the 5 second delay. Any advice is appreciated.
private ParticleSystem instantiate(ParticleSystem prefab, Vector3 position)
{
ParticleSystem newParticleSystem = Instantiate(
prefab,
position,
Quaternion.identity
) as ParticleSystem;
if(newParticleSystem.gameObject != null)
{
Destroy(
newParticleSystem.gameObject,
newParticleSystem.startLifetime
);
}
return newParticleSystem;
}
Your code relies on whateveris called ParticleSystem to keep track of when to Destroy the system. What I would do is this:
private ParticleSystem instantiate(ParticleSystem prefab, Vector3 position)
{
ParticleSystem newParticleSystem = Instantiate(
prefab,
position,
Quaternion.identity
) as ParticleSystem;
newParticalSystem.AddComponent<TimedDestroy>().delay = newParticleSystem.startLifetime;
return newParticleSystem;
}
and then add this script to your project:
using UnityEngine;
public class TimedDestroy : MonoBehaviour
{
public float delay;
void Start()
{
Invoke("destruct",delay);
}
public void destruct()
{
Destroy(gameObject);
}
}