- Home /
Restart Level, No restart Timer
Hello,
I´m learing a little scripting (wihh Unity/JavaScript) and its good till now. I have made a countdown-timer for my simple side-scroller game.
The Problem is, that I don´t know, how to reload the level (thourgh respawning) without to restart the timer.
So far this is the timer-script:
pragma strict
//A simple countdown timer var myTimer : float = 10.0;
function Update () {
if(myTimer > 0){
myTimer -= Time.deltaTime;
}
if(myTimer <= 0){
myTimer = 0;
Application.LoadLevel("GameOver");
}
}
function OnGUI(){
GUI.Box(new Rect(10,10,50,20),"" + myTimer.ToString("0"));
}
Does anyone have a solution?
What do you mean by "reload the level (thourgh respawning) without to restart the timer"?
From what I could understand, you can define a function "GameOver", run it if the timer goes below 0, and inside GameOver function, you can use Application.LoadLevel("TheNameOfTheLevel");
If the player is falling from the plattform, the player will respawn in the begnning zone again.
The Problem is, that the the timer will is also restarting, which it shouldn´t be for me.
Oh, by the way i understood , you mean to say when player falls off the platform you want to end the game within that many seconds right?
No. If the player falls off the plattform, the game will continue. But the the player will be respawn with the latest time on the beginning of the level.
there is a function called DontDestroyOnLoad which is very useful
Answer by spraw · Jul 01, 2014 at 03:24 PM
You will either have to save the time down to say PlayerPrefs and reload that upon restarting, or (based on your comment on what you're trying to archive) just move the player back to the original position as opposed to fully reloading the level.
$$anonymous$$oving the player to the original position worked for me! I've been killing myself trying to figure this out for days now. Thanks!
Answer by NorthernVisionStudio · Jul 05, 2014 at 07:44 AM
Two opinions, considering "reload the level through respawning" means that the level data in Unity remains, but the player respawns.
A) make the timer a static variable
or
B) separate the timer into a different game object from the player game object. Therefore, the player can respawn and the timer lives on
Your answer
![](https://koobas.hobune.stream/wayback/20220613152927im_/https://answers.unity.com/themes/thub/images/avi.jpg)