i'm usinga countdown timer script, which keeps going on and on and on... how to restart the level if the time is over? (0:00)
When the timer reaches zero (I'm assuming you have that set up already), simply do this:
Application.LoadLevel(Application.loadedLevel);
That will "restart" the current scene (excluding any persistent game objects which have `DontDestroyOnLoad` set on them).
Try adding this to your script:
if (restSeconds == 0)
{
Application.LoadLevel(loadedLevel);
}
Make sure you make a variable to tell unity what the loaded level is. ex:
var loadedLevel : int;
loadedLevel = 4;
//for example 4
I hope this helps! :)
This is what I’m using for my game, and it works just fine.
private var startTime;
private var restSeconds : int;
private var roundedRestSeconds : int;
private var displaySeconds : int;
private var displayMinutes : int;
var countDownSeconds : int;
function Awake() {
startTime = Time.time;
}
function OnGUI () {
//make sure that your time is based on when this script was first called
//instead of when your game started
var guiTime = Time.time - startTime;
restSeconds = countDownSeconds - (guiTime);
//display messages or whatever here -->do stuff based on your timer
if (restSeconds == 60) {
print ("One Minute Left");
}
if (restSeconds == 0) {
Application.LoadLevel("What ever you're scene is called");
}
//display the timer
roundedRestSeconds = Mathf.CeilToInt(restSeconds);
displaySeconds = roundedRestSeconds % 60;
displayMinutes = roundedRestSeconds / 60;
text = String.Format ("{0:00}:{1:00}", displayMinutes, displaySeconds);
GUI.Label (Rect (400, 25, 100, 30), text);
}
Use:
Application.LoadLevel(1); //where 1 is the number of the level in build settings.