- Home /
On death show onGUI and wait 5 seconds to load next level
When the player gets destroyed I want to show a Game Over OnGUI and wait for 5 seconds to load the next scene (restart/quit/addscore scene).
void OnDestroy()
{
Application.LoadLevel(Application.loadedLevel + 1);
}
What did you try? There is OnGUI documentation and Coroutine documentation with examples
Answer by HarshadK · Jun 11, 2014 at 01:44 PM
All you need is a coroutine.
bool showGUI = false;
void OnDestroy()
{
StartCoroutine("LoadNextLevel");
}
void OnGUI() {
if(showGUI) {
// Show the content of Game Over GUI
} else {
// Hide the Game Over GUI
}
}
IEnumerator LoadNextLevel() {
showGUI = true;
yield return new WaitForSeconds(5);
showGUI = false;
Application.LoadLevel(Application.loadedLevel + 1);
}
this is my code now...
bool showGUI = false;
public GUIStyle Font;
void OnDestroy()
{
StartCoroutine("LoadNextLevel");
//Application.LoadLevel(Application.loadedLevel + 1);
}
void OnGUI() {
if(showGUI) {
GUI.TextArea (new Rect(330,430,100,30), "g a m e o v e r", Font);
} else {
GUI.TextArea (new Rect(330,430,100, 30), "");
}
}
IEnumerator LoadNextLevel() {
showGUI = true;
yield return new WaitForSeconds(5);
showGUI = false;
Application.LoadLevel(Application.loadedLevel + 1);
}
it saws that IEnumerator does not exist in the current context
There is no error in the code since when I tried your code snippet it is not throwing any error. Just copy and paste the code again in the file and try it.
its fixed now but now so there are no areas but it does not show when my player dies. any ideaswhy?
the GUI never shows on the screen, and the next level is never loaded
Your answer
Follow this Question
Related Questions
WaitForSeconds vs yield every frame 3 Answers
yield WaitForSeconds not returning 1 Answer
Create a 10 second delay using WaitForSeconds 2 Answers
Animation there but not playing? 1 Answer
WaitForSeconds get stuck after update of Unity Pro 2 Answers