- Home /
New Particle System Destruction?
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?
Answer by chris gough · Apr 27, 2012 at 10:35 PM
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
You can also use the following line to automatically get the particle's duration and do the same as above:
Destroy(gameObject, particleSystem.duration+0.5f);
You can also launch a co-routine and pool particleSystem.IsAlive
Returns bool True if the particle system is still "alive", false if the particle system is done emitting particles and all particles are dead.
Answer by glitchers · Jul 24, 2014 at 02:09 PM
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 );
}
}
Answer by mattssheep4 · Oct 29, 2018 at 10:15 PM
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.
Your answer
Follow this Question
Related Questions
Change particles alpha / sharedMaterial 0 Answers
ParticleSystem.Play() does not play particle. 10 Answers
Particle bug... wrong location. Simple code 0 Answers
Get all particles within trigger collider? 1 Answer
Plasma Thruster Effect (Particles?) 1 Answer