- Home /
Reverting to Menu screen after game over
In my game basically if the player dies, the screen says "game over". Now i want it to go to the main menu either by waiting 3 seconds or by clicking retry, only thing is i do not know how to do that. I did try to add an application.loadlevel and yield it for 3 seconds but i got an error here is the script
function OnGUI(){
if (playerLives == 0 && player == null){
GUI.Box(Rect(0,0,Screen.width, Screen.height), "Game Over");
spawner.GetComponent(Enemy_Spawn).isON = false;
yield WaitForSeconds (4);
Application.LoadLevel ("Menu");
}
i get an error talking about OnGUI() Cannot be a coroutine, i didnt know what that meant(Beginner). If anybody could help thank you
Answer by robertbu · May 17, 2014 at 05:51 AM
You cannot use 'yield' in callback functions that get called every frame. FixedUpdate, Update, and OnGUI are three functions you cannot use 'yield' in, and I believe anything callback with the string 'Stay' also is prohibited from being a coroutine. A solution is to move your functionality to its own function:
function WaitAndDie() {
yield WaitForSeconds(4);
Application.LoadLevel ("Menu");
}
Now you just call this function. Note you need to structure your code so that the function is only called once. That may be you put a boolean flag in your code and set once this function has been called.
Alright I understand so put this after function onGUI and call it once