x


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?..

more ▼

asked Feb 21, 2012 at 10:20 PM

MickeyBlumental gravatar image

MickeyBlumental
31 1 2 2

(comments are locked)
10|3000 characters needed characters left

11 answers: sort voted first

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);
        }
    }
}
more ▼

answered Jun 12, 2012 at 05:36 AM

Jenster gravatar image

Jenster
91 2 2

In C#

using UnityEngine;
using System.Collections;

public class ParticleSystemAutoDestroy : MonoBehaviour 
{
    private ParticleSystem ps;


    public void Start() 
    {
        ps = GetComponent<ParticleSystem>();
    }

    public void Update() 
    {
        if(ps)
        {
            if(!ps.IsAlive())
            {
                Destroy(gameObject);
            }
        }
    }
}
Jun 16 at 07:32 PM RKSandswept
(comments are locked)
10|3000 characters needed characters left

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);

more ▼

answered Feb 22, 2012 at 08:23 PM

fireomega gravatar image

fireomega
391 21 14 20

If this answered your question please click on the tick

Feb 22, 2012 at 09:17 PM fireomega

thanks fireomega, that was perfect for me as I'm still using u3.4. When my missile object (that has the particle system attached to it) gets a signal to explode, this is how I handle the smoke trail:

particleSystem.particleEmitter.emit=false; 
particleSystem.transform.parent=null; // detach particle system
Destroy(particleSystem.gameObject, 3);
Destroy(gameObject);
Mar 16, 2012 at 08:34 AM masterchafe
(comments are locked)
10|3000 characters needed characters left

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);
more ▼

answered Mar 08, 2012 at 12:14 AM

RogueHydra gravatar image

RogueHydra
46

This is the simplest solution, without the need to attach scripts. I'd make one small change to the duration though to ensure no particles are cut off in their prime:

Object.Destroy(gameObject, psys.duration + psys.startLifetime);

Sep 06, 2012 at 09:40 PM _MGB_

This is even simpler. Only 1 line of code.

Destroy(gameObject, gameObject.particleSystem.duration);

Dec 06, 2013 at 08:49 PM windterfresh

The solution from MGB and windterfresh works perfectly, and makes for a very simple and reliable auto destruct component that can be added to any particle system object.

Here is the complete script:

using UnityEngine;
using System.Collections;

public class ParticleSystemAutoDestroy : MonoBehaviour {

    // Use this for initialization
    void Start () {
        if(gameObject.particleSystem)
        {
            GameObject.Destroy(gameObject, gameObject.particleSystem.duration + gameObject.particleSystem.startLifetime);
        }
    }
}
Jan 18 at 08:40 PM KevinCodes4Food

Thanks KevinCodes4Food! The solution you added above worked perfectly for what I needed! I am no longer getting dead particle systems hanging around wasting resources :]

Feb 25 at 07:35 PM Regretfulone
(comments are locked)
10|3000 characters needed characters left

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);

}

more ▼

answered Mar 08, 2012 at 12:14 AM

RogueHydra gravatar image

RogueHydra
46

(comments are locked)
10|3000 characters needed characters left

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);
}
more ▼

answered Mar 07, 2012 at 11:46 PM

Imi gravatar image

Imi
16 6 4 5

I would recommend to use LateUpdate() instead of Update(). Thanks for your code anyway. ;)

Mar 18, 2012 at 11:56 PM Dakwamine
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1008
x190

asked: Feb 21, 2012 at 10:20 PM

Seen: 23654 times

Last Updated: Jun 16 at 07:32 PM