I’ve noticed that in the newest version of Unity, there is not an auto-destruct option for the new particle system. My particles do not loop and are one-shot, but are still visible in the hierarchy after they’ve played.
Does this cause any sort of performance issue down the road if you’re working on a project that uses 20+ prefab explosions per minute? If so, how can you make sure that the particle system is removed after it plays?
create a script that simple says:
function Update(){
Destroy(gameObject, X);
}
X being just a little longer than how long the particles last, so if it was a 5 sec particle system X would be 5.5 seconds
Here is a script I wrote for this, it uses a delay with a IsAlive
coroutine
using UnityEngine;
using System.Collections;
[RequireComponent( typeof( ParticleSystem )) ]
public class ParticleAutoDestroy : MonoBehaviour {
public float autoDestructCheckInterval = 0.5f;
void Start () {
if( particleSystem.loop )
{
Debug.LogError ("Particle system can't self destruct if it loops");
}
else
{
StartCoroutine( TickDelay() );
}
}
private IEnumerator TickDelay()
{
yield return new WaitForSeconds( particleSystem.duration );
StartCoroutine( AliveCheck() );
}
private IEnumerator AliveCheck()
{
while( particleSystem.IsAlive() )
{
yield return new WaitForSeconds( autoDestructCheckInterval );
}
Destroy( gameObject );
}
}
For those using Unity 2018 (Probs 2017 and up, but can’t confirm), there’s a handy “Stop Action” drop down which tells the particle system what to do once it stops and all particles have died. This includes an option to destroy itself. In theory this is a better alternative than a custom script.