- Home /
Countdown problem
Hello, I have a little problem with my countdown script.
My game has a countdown that if it reach 0, this will freeze the game and you have to Press R to reload the scene, it works but after it reload the scene, the scene is still frozen, Can anyone help me please?
I know that Time.timeScale = 0; is causing the problem, It may be a little thing but Im noob scripting. Help me please.
var timer: float = 300; // set duration time in seconds in the Inspector
var isFinishedLevel : boolean = false; // while this is false, timer counts down
function Update()
{
if (!isFinishedLevel) // has the level been completed
{
timer -= Time.deltaTime; // I need timer which from a particular time goes to zero
}
if (timer > 0)
{
guiText.text = timer.ToString("F0");
}
else
{
guiText.text = "Press R to restart"; // when it goes to the end-0,game ends (shows text: time over...)
Time.timeScale = 0;
if (Input.GetKeyDown("x")) // And then i can restart game: pressing restart.
{
Application.LoadLevel ("as"); // reload the same level
}
}
}
Can't you just insert:
Time.timeScale = 1.0;
...at line 23?
Just try something like this (keep in $$anonymous$$d this is C# I'm posting below, not JavaScript)
public float timer;
public float currentTimer;
public bool doCountdown;
void Start() {
currentTimer = timer;
}
void Update() {
if(doCountdown) {
if(currentTimer <= 0) {
//BEEP BEEP BEEP
}
else
currentTimer -=time.deltaTime;
}
}
With this you can reset the timer too by using currentTimer = timer;
Answer by rutter · Jul 27, 2014 at 05:53 AM
Just reset the timeScale
to 1 when you reset.
You could do this right before calling Application.LoadLevel
, or in a Start
or Awake
function after the level loads.