- Home /
How do I give an option to retry a level or return to a level selection menu opposed to just restarting the level?
I would like to give an option to either retry or go to main menu. I am brand new to coding and unity so simple basic explanations would be greatly appreciated. I would assume a "waitForSeconds" would be needed before the option appears, however I do not know how to implement that in my current scrip and this is why I have implemented application.LoadLevel(Application.loadedLevel);. instead. As you will notice the first two public game objects are not put into use simply because Im unsure how at this time. Here is the script that I have for now. Please do not be rude. I have hesitated to ask this question for the last 2 days simply because I don't want someone to make a newbie feel like an idiot.
using UnityEngine; using System.Collections;
public class TimeText : MonoBehaviour { public GameObject gameOverText; public GameObject restartButton; public GUIText timeText; public float timer = 30.00F;
// Update is called once per frame
void Update ()
{
timer -= Time.deltaTime;
timeText.text = "Remaining Time " + timer.ToString ("0.00");
if (timer <= 0) {
timeText.text = " Score";
Time.timeScale = 0;
Application.LoadLevel(Application.loadedLevel);
}
}
}
Answer by zaid87 · Aug 15, 2014 at 09:37 AM
Well, I'm kinda newbie to Unity too, so hi fella.
Anyway, I would suggest you create a state system for controlling your game's state. When inside the "game over" state, you display the buttons for restarting the level or going back to main menu. I think it would also be better to make a different Scene for the main menu, this way you can use the Application.LoadLevel() to load between main menu and in-game. For example :
enum GAMESTATE {PLAYING=0, GAMEOVER};
function Start() {
GAMESTATE currentState = GAMESTATE.PLAYING;
}
function OnGUI() {
if (currentState == GAMESTATE.GAMEOVER){
if (GUI.Button(new Rect(0, 0, 50, 20), "Retry")) {
Application.LoadLevel(Application.loadedLevel); // restart the level
}
if (GUI.Button(new Rect(0, 0, 50, 20), "Exit")) {
Application.LoadLevel(0); // go back to main menu
}
}
}
The LoadLevel(0) is assuming the index of main menu's scene is 0, you can set this in the Build Settings. Hopefully this can help you a bit.