JGrn84
JGrn84

Reputation: 750

Particle system DontDestroyOnLoad

What I'm after in my game is when a collision happens with the player and a planet, for the player to disappear, leaving behind an explosion effect in the form of a particle system. Just afterwards (maybe half a second) I want the "game over" scene to appear in its place. Here's what I have so far:

void OnCollisionEnter2D (Collision2D col) {
    if (col.gameObject.tag == "enemyPlanet") {
        Instantiate (explosion, thingToMove.transform.position, thingToMove.transform.rotation);
        ui.gameOverActivated ();
        Destroy (gameObject);
        am.rocketBang.Play();
        Application.LoadLevel ("gameOverScene2");
    }
}

The problem I have is that the particles appear but don't move as they should like an explosion. I guess that's either because the game over scene is loading or because its position is the player (thingToMove) which is being destroyed.

I tried this:

public void Awake() {
    DontDestroyOnLoad (transform.gameObject);
}

But the same thing happens. If it's because the player is being destroyed, how would I make it be in the place of the player at the time of it being destroyed?

I hope this makes sense and thanks in advance.

Upvotes: 1

Views: 267

Answers (1)

Milad Qasemi
Milad Qasemi

Reputation: 3059

You can use the Invoke method to call the game over after a specified delay.

void OnCollisionEnter2D (Collision2D col) {
    if (col.gameObject.tag == "enemyPlanet") {
        Instantiate (explosion, thingToMove.transform.position, thingToMove.transform.rotation);
        ui.gameOverActivated ();
        am.rocketBang.Play();
        Invoke( "over", 2.0f );

    }
}

void over(){
    Destroy (gameObject);
    Application.LoadLevel ("gameOverScene2");
}

Upvotes: 2

Related Questions