Auto-destroying particle system

I’m going through the 3D Buzz 2D shooter tutorial series and they obviously use an earlier version of Unity as the options for particle systems has changed beyond recognition. In the older version there was a tick box to tell the particle system to auto-destroy once it’s finished playing. The new version of Unity doesn’t have that option and as a result every time I kill an enemy an empty particle system is left behind after the explosion finished playing, which is obviously not good.

How do I destroy them now?..

I have some particle systems that are one-shots, so here is what I do to auto-destroy them when they are complete. IsAlive() is the key method. Just attach this script, fire off the system any way you like, and it will autodestroy.

private var ps : ParticleSystem;

////////////////////////////////////////////////////////////////

function Start () {
         ps = GetComponent(ParticleSystem);
}

function Update () {
	if(ps)
	{
		if(!ps.IsAlive())
		{
			Destroy(gameObject);
		}
	}
}

A simple Destroy code and then just add the time the particle lasts for. e.g. say the time it lasts for is 5 seconds than you do:
Destroy(gameObject, 5);

I saw this and made a minor adjustment. It works for me in my situation but I’m fairly new to using Unity so I’m not sure how valid it will be for anyone else. This lets you put it on any particle system and it will kill it when it’s done playing (no need to set the duration).

var psys = this.GetComponent(ParticleSystem);
Destroy(this.gameObject, psys.duration);

I modified it just a little so you can just attach this to any particle system and it’ll kill it after the duration is up. I’m a not sure if this’ll work for everyone but it works perfect for me and all my systems (haven’t had any issues and haven’t seen any bugs yet).

function Start () {

var psys = this.GetComponent(ParticleSystem);

Destroy(this.gameObject, psys.duration);

}

I recommend the following script, as it does not need to know how long the particle system will go (this can vary depending on the start lifetime and in worst case a lot of curves…)

function Update ()
{
  if (particleSystem != null && particleSystem.particleCount == 0)
    Destroy(gameObject);
}

I do not agree with all the rest of the answers. I think the most effective way is to use a WaitForSeconds term to Destroy the GameObject after a few seconds of instantiation. Do it whatever way you like, but this is best for me. I use it with fireballs and it works perfectly fine. Or, you can set a trigger around the player and set up the particle to destroy in the OnTriggerExit function in unity’s built in Monobehaviour inheritance system.

Attach this script to ur particle

function Start ()

{
Destroy(gameObject, time eg 2);

}

when ur particle on after 2 second it destroy automatically .

As logical as the !ParticleSystem.IsAlive method seems, it never worked for me. Unity would run any code inside an “if .IsAlive" statement, but something was keeping the particle system alive so that it would never run code in an "if !.IsAlive” statement even though the it was not emitting particles and had no existing particles (which is the only requirements for the function, according to unity’s scripting reference).

So eventually, to circumvent this problem, I created my own if statement which checked these for two requirements without using the IsAlive function. This script attached to any GameObject (with a Particle System) will destroy said GameObject if its emitter is disabled and all its particles are dead. (I only use it for Particle System GameObjects…):

`
private var emitter : ParticleSystem;

function LateUpdate() {
emitter = GetComponent(ParticleSystem);

//if(!emitter.IsAlive()) //!IsAlive won’t work for some reason

if((emitter.particleCount == 0) && (emitter.enableEmission == false)) // does what !IsAlive should do…
Destroy(gameObject);
}
`

The duration is the amount of time the particle system EMITS particles, but due to the lifetime of each particle, there can still be particles in space after the duration time.
Instead of just duration, destroy the particle system after duration + startLife, so that the remainder particles have time to disappear on their own. This is necessary if you have particles with a long lifetime

I’ve had the same issue with isAlive() what happens is it never checks if the system already played or not. You can use this code (C#):

using UnityEngine;
using System.Collections;

public class SelfDestructingParticleControl : MonoBehaviour {
	
	ParticleSystem ps;
	bool played;
	
	void Start () {
		ps = gameObject.GetComponent<ParticleSystem>();
	}
	
	void LateUpdate () {
		if(!played && ps.isPlaying) {
			played = true;
		}
		if(played && !ps.IsAlive(true)) {
			Destroy(gameObject);
		}
	}
}

I have the similar problem as yours and I got it resolved. Below is the text extract from this post.


In Unity3d 4, when I create a particle system (GameObject > Create Other > Particle System), I am not yet complete the creation of a particle system. I need to add additional effect component in order to make it work. For my case, I am making an explosion effect, thus I need:

  1. Ellipsoid Particle Emitter (Component > Effects > Legacy Particles)
  2. Particle Animator (Component > Effects > Legacy Particles)

Inside the Particle Animator, remember check on the Autodestruct checkbox to clear the particle system from memory once it finish rendered.