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;
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.
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.
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
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?
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);
}