- Home /
How to restart a level with countdown?
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)
Answer by TaigaStudios · Jul 13, 2010 at 08:30 AM
Use:
Application.LoadLevel(1); //where 1 is the number of the level in build settings.
Why hard-code the level number when you have Application.loadedLevel which returns the currently loaded level?
THAN$$anonymous$$S :) now i need to work out a splash screen loader before the level restarts :)
Oh sure, accept the answer with the worse program$$anonymous$$g solution... (what is this world co$$anonymous$$g to...)
"Fire and brimstone co$$anonymous$$g down from the skies! Rivers and seas boiling! Forty years of darkness! Earthquakes, volcanoes... The dead rising from the grave! Human sacrifice, dogs and cats living together... mass hysteria!" - Or was that a rhetorical question, @SpikeX?
@Cyclops Good one, secondly SpikeX's answer was better than $$anonymous$$e, which was copied in haste, without giving it much tought. We should always try to avoid hard-coding things when possible, but is it end of the world even though we didn't? Also SpikeX gave short explonation and I didn't votes up
Answer by qJake · Jul 13, 2010 at 08:34 AM
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).
Answer by Chris 4 · May 02, 2011 at 07:40 PM
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! :)
Answer by RickHurd · May 04, 2013 at 11:40 PM
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);
}