- Home /
The question is answered, right answer was accepted
Game Timer help needed
Attempting to make a script that runs a timer that keeps track of how long you survived in game, and also updates the highscore if the timer is larger than it. This all works perfectly right now and i have achieved the above, but now I've been trying to have the game and timer reset when you lose, the only problem is that the timer just pauses and then upon reset carries on from where it left off. I need to set the value of the timer to zero but this doesn't seem to work, any help??
For the GUI to display the Timer
public class Timer : MonoBehaviour {
//Reference for timer layout
public int minutes;
public int seconds;
public int fraction;
//Timer
public static float countTime;
//bool "go" is available to use by other scripts also
public static bool go;
void Start(){
go = true;
}
void Update ()
{
//Only happen if go = true
//Triggers and displays the time with correct format upon go = true
if(go) {
countTime = Time.time ;
minutes = (int)(countTime/60f );
seconds = (int)(countTime % 60f);
fraction = (int)((countTime *10) %10);
guiText.text = string.Format("{0} . {1} . {2}", minutes, seconds, fraction);
}
}
}
For a seperate GUI to display a highscore as explained above
public class Bestscore : MonoBehaviour {
//For best time public int minutesB; public int secondsB; public int fractionB;
//Defines a public float "bestTime" public float bestTime = 0f;
void Start() {
//On startup PlayerPrefs are loaded
bestTime = PlayerPrefs.GetFloat("bestTime", 0f);
}
//At the end of the game run below
void OnDestroy() {
//Saves the data for later use
PlayerPrefs.SetFloat("bestTime", bestTime);
}
void Update() {
//If score is higher than the high score the timer will match the score
if(Timer.countTime > bestTime) {
bestTime = Timer.countTime;
}
}
void FixedUpdate() {
//Layout for high score GUI text
minutesB = (int)(bestTime/60f );
secondsB = (int)(bestTime % 60f);
fractionB = (int)((bestTime *10) %10);
guiText.text = string.Format("BEST TIME : {0} . {1} . {2}", minutesB, secondsB, fractionB);
}
}
I've tried adding countTime = 0f; to the start but that dosen't do anything. Sorry if this is too long or troublesome, any help would be appreciated and i'll be on in the evening to check back in.
This is a SERIOUSLY well documented question.
It's so duplicated that its like The Borg. There is absolutely nothing about timers that you can't find in a thousand different existing QA's on Unity Answers. Please search and do your research before posting!
No offense intended.
The only reason this question is not closed is that 3 people have taken the time to answer. I won't do them the mis-justice.
Answer by Phles · Jun 16, 2014 at 03:52 PM
Hey,
Instead of using Time.time
increment countTime by Time.deltaTime
;
countTime += Time.deltaTime;
You can then simply do countTime = 0; to rest the timer, Time.time
is the seconds since the game started you don't want to use that here.
Phill
This worked perfectly, thanks so much for suggesting it, whether or not the question seemed stupid i really appreciate the help!
Could you try Unaccepting and reaccepting the answer? Seems to have bugged out in the list.
Answer by Minchuilla · Jun 16, 2014 at 03:42 PM
What I would do is this:
use a variable to decide when the timer has been started
ie.
//when button is clicked or whatever set timerStarted to true
if(timerStarted)
{
timeOfStart = Time.time;
timerStarted = false;
}
if(IAmTiming)
{
float currentTime = Time.time - timeOfStart;
minuites = (int)(currentTime/60);
//etc
}
Then each time you want to reset the timer set timerStarted to true
Feel free to ask for more detail or prove me wrong.
Minchuilla
I'll try out some of these and see if i can get it working, thanks for the feedback. I'll let you guys know how it goes.
Follow this Question
Related Questions
How to work a real life timer? 2 Answers
How to make a Time Based Score? 1 Answer
Timer doesn't work properly 2 Answers
How to make a timer read to the .001 of a second 2 Answers
Multiple Cars not working 1 Answer