- Home /
Reset the Time.time back to 0.0?
Hi I have a zombie like game and has a few scenes a title, the game, instructions, and scene after you died. In the game i have an extremely simple timer script that shows how long you have survived using Time.time. When you die you get taken to a scene that shows how long you have survived, a try again GUI.button, and a return to title GUI.button. If you click the try again one the time will not reset it will stay where you left off. Is there a simple way to reset it back to 0. I have tried making a boolean that is true if you clicked try again and in the timer script if it is true set the time to 0.0 but the time will just stay at zero because i didn't make it false again. Please help.
The Timer script
static public var time : float;
function Update(){ time = Time.time; }
function OnGUI(){ GUI.Label (Rect (52,85, 200, 200), "Time = "+ time); }
The Death scene script
pragma strict
var customSkin : GUISkin; var timeAlive : float;
function Update(){ timeAlive = Timer1.time; }
function OnGUI () { var buttonWidth : int = 400; var buttonHeight : int = 150;
var halfScreenWidth : float = Screen.width / 2;
var halfButtonWidth : float = buttonWidth / 2;
GUI.skin = customSkin;
if(GUI.Button(Rect(halfScreenWidth - halfButtonWidth, 150, buttonWidth, buttonHeight), "Try Again")){
Application.LoadLevel("ZombieHunt");
}
if(GUI.Button(Rect(halfScreenWidth - halfButtonWidth, 450, buttonWidth, buttonHeight), "Title Screen")){
Application.LoadLevel("TitleZombie");
}
GUI.Label(Rect(halfScreenWidth - 250, 45, 500, buttonHeight), "You Survived for "+timeAlive+" seconds");
}
please help also if you have any suggestions to make it better I have played with unity for a while and know most things but not all.
Answer by rutter · Jun 16, 2014 at 06:48 PM
You can't reset Time.time
, but I do see two options that will work just as well.
First option, probably the simplest, is to just use Time.timeSinceLevelLoad:
function Update() {
time = Time.timeSinceLevelLoad;
}
Second option, a little bit harder but lets you start the timer whenever you want, is to use Time.time
with an offset:
function Start() {
startTime = Time.time;
}
function Update() {
elapsedTime = Time.time - startTime;
}
Thank I just changed Time.time to Time.timeSinceLevelLoad and it worked perfectly.
Awesome - thanks :) Had my head stuck in a "loop" of complicated solutions - this is awesome and simple, thanks again.
sorry, i don't get it, what is "time" reference in the start function? when i try it says 'time' does not exist in current context
If I follow the second option, does anyone know why startTime remains at 0?
Should Time.time ever be placed inside the Start() function?
Time.time gives you how long the scene have been running for (https://docs.unity3d.com/ScriptReference/Time-time.html)
The whole point of the second option is that you have the startTime (which is 0 at the start of the scene) and then subtract this from the current time. (So 4 $$anonymous$$utes into the game, the elapsed time will be 4$$anonymous$$utes - 0)
So, if at some point you want to set a new "point in time" that you want to set as a new startingpoint... you simply do startTime = Time.time; again.. and the elapsedTime will show you the elapsed time since you set it. Example: 3 $$anonymous$$utes after the scene loaded, the player starts the game by clicking a "Start Game" button, if you now set the startTime = Time.time; - startTime will be 3 $$anonymous$$utes. When the scene have now been loaded for 4 $$anonymous$$utes - elapsedTime = 4 $$anonymous$$ - 3 $$anonymous$$ ... hope this makes sense.
Thank you Tabu - I think I'm slowly getting it, but I'm still a little confused!
So Time.time stores the amount of time that has passed since the game has run - Time.time is read-only so it cannot be changed. Because of this, if we ever want to 'play' with time, we must create a new float variable that references the value of Time.time so that we can modify it (for instance, resetting its value to 0).
By creating a variable that stores Time.time that is placed in void Start(), the value will always remain at 0 because Start() only runs once (at the very beginning) and doesn't loop like void Update().
In the Update() function, we create another variable that stores the amount of time that has passed. So like you said, if 4 $$anonymous$$utes have passed, then Time.time - startTime equates to this: 4 - 0. Which means that the value of elapsedTime is 4.
I think I'm confused by your example. If I understood correctly, in the scenario you created, you want to re-start the time when 'Start Game' button is pressed (so reset the time at 0). If 4 $$anonymous$$utes have passed, why would the elapsedTime be 1 $$anonymous$$ute (4$$anonymous$$ - 3$$anonymous$$)?
Another question I have is what happens when we reset elapsedTime to 0 and call Time.time again. Does Time.time begin at 0 again? Or will it's value remain as the amount of time that has passed?
Hope you can clarify for me - thank you again!
Answer by MikeNewall · Jun 16, 2014 at 06:50 PM
Time.time is the time from the start of the game. It's read only and it will only reset when the game restarts.
Store your own time as a float. If you add Time.deltatime to it each frame it will count in seconds and you can set it back to zero whenever you like.
Answer by Content1of_akind · Apr 14, 2019 at 03:34 AM
I provided three optional methods. The concept is whether you want to have a timer, reset a timer or stop a timer . Hope this helps someone, Cheers!
public float timer = 0.0f;
public int seconds;
public bool keepTiming = true;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Timer();
//ResetTimer();
//StopTimer();
}
public void Timer()
{
// seconds
timer += Time.deltaTime;
// turn float seonds to int
seconds = (int)(timer % 60);
print(seconds);
}
public void ResetTimer()
{
// seconds
timer += Time.deltaTime;
// turn float seonds to int
seconds = (int)(timer % 60);
print(seconds);
if (seconds > 4)
{
// both code runs the same
//timer = Time.deltaTime;
timer = 0.0f;
}
}
public void StopTimer()
{
if (keepTiming)
{
// seconds
timer += Time.deltaTime;
// turn float seonds to int
seconds = (int)(timer % 60);
print(seconds);
}
if (seconds > 4)
{
// stop and record time
keepTiming = false;
print(seconds);
}
}
Answer by urii_baba · Oct 26, 2018 at 03:05 PM
I don't get the thing ! why do we have to subtract it from 0 (start time) what difference will it make ?
You're not subtracting 0 - you're subtracting the value of Time.time from itself to get 0.
Time.time is static, it can't be changed, if you get the value of Time.time as a variable and then subtract that amount to a new variable you will get 0, so effectively 'resetting' time to 0. If Time.time is currently at 15 seconds, and you want it to go back to 0, you have to subtract 15 - and the best way of doing that is subtracting the thing that holds that value - Time.time. :)