I am trying to destroy a game object and let its particles (of children) finish their last loop before destroying them.
void Update () {
if (Input.GetKeyDown(KeyCode.A)) {
Destroy(gameObject);
}
}
void OnDestroy() {
// Attempt 1 - one of the particle systems get left behind
ParticleSystem [] particles = gameObject.GetComponentsInChildren < ParticleSystem > () as ParticleSystem[];
Debug.Log (particles.Length); // FIXME: Why 0?
foreach (ParticleSystem p in particles) {
p.loop = false;
p.Stop();
}
/* // Attempt 2 - disables my autodestruct script
transform.FindChild("PS").particleSystem.Stop (true);
*/
transform.DetachChildren();
}
Unfortunately, they don’t stop emitting or they don’t get destroyed. The particle contains a self destruct script as follows:
void Update () {
if (particleSystem.IsAlive()) return;
Destroy(gameObject);
}
I don’t understand why GetComponentsInChildren() returns 0 length and why particleSystem.Stop() disables my scripts.
Thanks in advance!
[7326-particlesystemautodestruct.unitypackage.zip|7326]
EDIT: corrected line 9 to specify type of component to get from children.