- Home /
Resetting Time after Game Reload in iOS
How do I reset the time after the game fail? The time just keeps decreasing in my game even after the game over scene is played out and it restarts back to the main scene.
This is my script attached to the GUI for time:
#pragma strict
private var startTime;
var restSeconds : int;
private var roundedRestSeconds : int;
private var displaySeconds : int;
private var displayMinutes : int;
var countDownSeconds : int;
function Awake() {
startTime = countDownSeconds;
}
function Update ()
{
guiText.text = String.Format ("{0:00}:{1:00}", displayMinutes, displaySeconds);
}
function OnGUI () {
var startTime: float;
var guiTime: float = Time.time - startTime;
restSeconds = countDownSeconds - (guiTime);
if (restSeconds == 60) {
print ("One Minute Left");
}
if (restSeconds == 0) {
print ("Time is Over");
LevelFail();
}
roundedRestSeconds = Mathf.CeilToInt(restSeconds);
displaySeconds = roundedRestSeconds % 60;
displayMinutes = roundedRestSeconds / 60;
}
function LevelFail()
{
Application.LoadLevel("GameOver");
startTime = 240;
}
My countDownSeconds is set to 240 in the inspector. Thank you! :)
Answer by syclamoth · Sep 13, 2011 at 11:05 AM
You are counting down using Time.time, instead of detracting Time.deltaTime every frame. The problem here is that Time.time is absolute- which means that no amount of level loading and resetting will set it back to zero: just like in real life, time that is lost can never be recovered. Instead, you should put a countdown float at the top and decrease it by Time.deltaTime inside the Update function, then base all of your GUI stuff off it. Another thing, by redefining startTime at the top of OnGUI, you are actually hiding the startTime var elsewhere in your program, which I don't think you really want to do.
Sorry I don't really understand. Did you mean it like that?
function Update () { startTime= Time.deltaTime - countDownSeconds; guiText.text = String.Format ("{0:00}:{1:00}", display$$anonymous$$inutes, displaySeconds); }
But time did not re-set... As for re-defining the startTime, should I declare at the top or OnGUI() is fine? Thank you for helping. Cheers!
Time.time is a counter of the total number of seconds since the beginning of the game. If you define your countdown in terms of it, the countdown will continue to decrease even if you reset the level! Time.deltaTime, on the other hand, is the amount of time between the last time Update was called and now- so if you have a countdown float countdown, and then use
countdown -= Time.deltaTime;
once every Update, then it will decrease at a constant rate of one per second. This is a much more general way of implementing a countdown, and will reset automatically when you load the level!
But my countdown is an integer cause I want to set a fix 4 $$anonymous$$ute for the game.
Why does the countdown have to be an int? I don't understand this. There is no reason why it can't be a float ins$$anonymous$$d- if there's any time when you absolutely need it to be an integer you can just use $$anonymous$$athf.RountToInt(countdown) to turn it into an int temporarily.
Because the time starts at 4$$anonymous$$ and I would like to re-set it to 4$$anonymous$$ each time the scene loads.
Your answer
![](https://koobas.hobune.stream/wayback/20220612211741im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Time based scoring 2 Answers
Screensaver when device is idle 1 Answer
How do you change a sprite after an amount of time? 0 Answers
ResetTime doesnt work. help pls 1 Answer