Losing references with instantiate prefab

I’ve created a GameObject (renamed “Explosion”) with three childs. Every child has a different Particle System (Smoke, Sphere and Ring) to create an explosion effect and I want auto-destroy the parent GameObject “Explosion” and all childs once all Particle Systems finished playing.

To do this I’ve created this script “ManagerExplosion”

public ParticleSystem smoke;
public ParticleSystem sphere;
public ParticleSystem ring;

// Use this for initialization
void Start () {

    smoke = GetComponent<ParticleSystem>();
    sphere = GetComponent<ParticleSystem>();
    ring = GetComponent<ParticleSystem>();
}

// Update is called once per frame
void Update () {

    if (!smoke.IsAlive() && !sphere.IsAlive() && !ring.IsAlive())
    {
        Destroy(gameObject);
    }

Using another script, I create the instance

public GameObject explosion;

void Start () {
    Instantiate(explosion);
}

This is the GameObject “Explosion” and the script with the three Particle System that I’ve dragged

122738-bbbb.png

The issue is when I launch the game it Instantiate prefab correctly and all Particle Systems works but, as you can see, I lose all references Perticle Systems and the GameObject “Explosion” is not destroyed…

Obviously I get an error because there is not attached Particle System:
MissingComponentException: There is no ‘ParticleSystem’ attached to the “Explosion(Clone)” game object, but a script is trying to access it.

122739-cccc.png

So, what am I doing wrong?

Remove your Start method and the 3 GetComponent lines in it. You overwrite your 3 variables with null here since there is no particle system on the GameObject where your script is attached to.

Also calling 3 times GetComponent in a row will always return the same value anyways. Since you linked your objects in the inspector you don’t have to do anything with the variables