0
\$\begingroup\$

I am developing a simple Unity 2D game. During the development of Level One, I wanted the scene to continue after the player is triggered by the enemy and dead util the given 5 lives are completed. When I add Coroutine, it reloads the whole scene and I don't want that...I just want to continue its life from the last hit. So how can I make the scene continue until it loses the whole 5 lives?

void OnTriggerEnter2D (Collider2D col) {
    if (col.gameObject.tag == "Player" ) {
        health.CurrentVal -= 1;
        Explode ();
        if (health.CurrentVal != 0) {      
            StartCoroutine ("ReloadGame");
        } else {
            StopAllCoroutines();
        }
    }
}

Thanks!

\$\endgroup\$
1
  • 2
    \$\begingroup\$ You need to show your ReloadGame method. Also, this is probably a really bad use of coroutines. \$\endgroup\$ Commented Apr 28, 2017 at 15:53

1 Answer 1

1
\$\begingroup\$

tldr; Don't reload your scene if you don't want it to go away and start fresh.


This sounds like it has nothing to do with the concept of a Coroutine.

I assume you're reloading the current scene. Instead you should simply respawn your player. You can either:

  1. Reset its stats, animations, etc... and physically move him
  2. Destroy and re-create the player object at the spawn location

How exactly you do this is highly dependent on how you have your particular game set up. If you go with option #2 you'll need to ensure things like the number of lives you have aren't stored directly on a script attached to the player object else they'll get destroyed and reset as well.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .