[Solved] How do I destroy the smoke clones in Unity 2017?

Update: The code to destroy the smoke according to it’s duration is: Destroy(smokePuff, main.duration); at the end of the smokePuff function. Does anyone know to use isAwake() to destroy the smoke clones?

Update: Adding Destroy(smokePuff, 3f); at the end of the puffSmoke function destroys the clones, but how do I do it using isAlive or the particle duration?

When each block is broken, Unity creates another smoke clone. How do we destroy the object at the end of the animation?

The 4 lines of code to make the particle system color change were:

//this goes above start
public GameObject smoke;
//this goes in the puffSmoke() function that is called from handleHits()
GameObject smokePuff = Instantiate(smoke, transform.position, Quaternion.identity) as GameObject;
ParticleSystem.MainModule main = smokePuff.GetComponent().main;
main.startColor = gameObject.GetComponent().color;

1 Like

Hi Brady,

This IsAlive method returns a boolean value which you could query within an if statement. Providing true as a value for the withChildren parameter will check all child particle systems also.

If the method returns false, then the particle system is no longer emitting particles, and all of the child particles are also finished.


Example;

void LateUpdate () 
{
    if (particleSystem.IsAlive() == false)
        {
            Object.Destroy(this.gameObject);
        }
}

…or a shorter version;

void LateUpdate () 
{
    if (!particleSystem.IsAlive())
        Object.Destroy(this.gameObject);    
}

You could, for example, pop the above into a class and added it as a script to the main particles object prefab.

Hope this helps.


See also;

1 Like

Is particleSystem a built-in Unity thing or is that something that needs to be defined?

Hi Brady,

You would need to define it, as it is a component you add to a GameObject you could use GetComponent.

I’m still confused.

I’m not really sure what you’re trying to achieve.

Is it as simple as just unchecking ‘looping’ on the smoke’s particle system? It should be all controllable from there rather than in the code.

On which part Brady?

Brady wants to destroy the surplus game objects that are created (the clones) after they are no longer required @Brian_Robson. If you run BlockBreaker and watch the Hierarchy when the blocks are broken you will see, as default, the “smokePuffs” remain even after the particles are finished.

Obviously, this all gets cleared down anyway when the next scene loads, but good housekeeping is a good practice.

At least for the destroying using the particle duration, I used this:

Destroy(smokeRain , smokeRain.GetComponent().duration + smokeRain.GetComponent().startLifetime);

(My puffSmoke is named smokeRain, because it’s more rainlike :wink: )

Ohh… I see…

I didn’t actually notice that! And yeah, I’d imagine each one takes up a bit of memory unnecessarily…

1 Like

Exactly :slight_smile:

Always best to clean up after yourself, whilst with a small game like this it may make very little difference, part of the bigger picture is being in the practice of identifying and resolving these little things - saves a huge task of trying to track things down later :slight_smile:

I’m using Unity 2017.1.0p4, I have to call:

ParticleSystem.MainModule main = smokePuff.GetComponent().main
Destroy(smokePuff, main.duration);

I was trying to figure out how to use IsAlive(); with this code implementation.

I may be totally wrong, but what would the IsAlive() be good for?

You are destroying the brick gameobject right after generating the particle object and have no further access to the particle object.
Wouldn’t you have to attach a script to the Smoke prefab and do the testing there?

Right. How do I do it with Unity 2017?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Smoke : MonoBehaviour {
private ParticleSystem ps;

// Use this for initialization
void Start ()
{
   ps = GetComponent<ParticleSystem>();
   ps.Emit(1000);
}

// Update is called once per frame
void Update ()
{
    ps = GetComponent<ParticleSystem>();
    if (!ps.IsAlive()) Destroy(gameObject);

}

}

Attach to smoke. Worked for me.