- Home /
Second Coroutine isn't working Unity
I am making 2D game and want to cause delay of about 3 sec after player's all lives ran out. I tried to implement Coroutine method before the scene start all over again but it doesn't work. I have already implemented Coroutine method for each time my player falls of a cliff and respawn back to its position. And it works like a charm.
public void Respawner() { StartCoroutine("RespawnCoroutine"); }
// Coroutine Delay of 2 sec for each time player Respawn
public IEnumerator RespawnCoroutine()
{
classobj.gameObject.SetActive(false);
yield return new WaitForSeconds(respawnDelaySec);
classobj.transform.position = classobj.respawnPoint;
classobj.gameObject.SetActive(true);
}
public void ReduceLives() {
if (lives <= 3 && lives >= 2) { lives--; live_text.text = "Remaining Live " + lives; }
else {
StartCoroutine("RestartScene1");
}
}
public IEnumerable RestartScene1()
{
yield return new WaitForSeconds(RestartSceneDelaySec);
SceneManager.LoadScene("demo2");
}
here is no error on console window but SceneManager.LoadScene("demo2"); is never called and the player is respawning each time after i die and after 1 life remaining
Answer by b1gry4n · Jun 02, 2018 at 02:42 AM
change IEnumerable
to IEnumerator
on your RestartScene1 coroutine.
lol i feel like an idiot now, thanks for helping me out sir. But there is one slight problem now, Respawn delay is set to 2 sec and Restart_scene set to 3 sec. When all lives run out the player respawn again for 1 sec and then the scene restarts again. which is kinda annoying i want it to completely pause or end scene when all lives run out. And then start scene again. Is there a code for pausing scene?
you could just call either coroutine in the same function, then it is 100% guaranteed to be one or the other
if (lives > 1)
{
lives--;
live_text.text = "Remaning Lives: " + lives;
StartCoroutine("RespawnCoroutine");
}
else
{
lives = 0;
live_text.text = "Remaning Lives: " + lives;
StartCoroutine("RestartScene1");
}
Thanks you very much.. that really worked :) Just made me realize that there are simple way of doing things. Before that i was calling respawn method from another script.. didnt realize i could call from same public void ReduceLives() function