- Home /
The question is answered, right answer was accepted
Timer Script Not Keeping Accurate Time?
I have this C# timer script and I only just noticed that for some reason it is NOT keeping accurate time. For instance in the seconds part of the timer it should change the minute number over when it reaches 60 seconds but it does not? Any clue as to why that is? Anyone. Also in one of my previous posts I was trying to figure out how to get this timer to keep time over scene changes, so far no luck with that question either. Here is the timer code:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class timer : MonoBehaviour {
public Text timerLabel;
private float time;
void Update() {
time += Time.deltaTime;
var minutes = time / 60; //Divide the guiTime by sixty to get the minutes.
var seconds = time % 60;//Use the euclidean division for the seconds.
var fraction = (time * 100) % 100;
//update the label value
timerLabel.text = string.Format ("{0:00} : {1:00} : {2:00}", minutes, seconds, fraction);
}
//Reset Timer
public void ResetTimer(){
time = 0;
Debug.Log("Timer Reset");
}
//Stop Timer
public void StopTimer(){
//Stop Timer Here
Debug.Log("Timer Stopped");
}
}
I have been looking into trying to add an hours slot to this script, I have tried a few things but not having any luck, my lame experiments with it get strange results. I'm pretty sure it just needs another Var added and then the additional time slot on line 25 but I'm not getting clearly how that is done and the only other examples I have seen are in a differently formatted code.
Answer by Jessespike · Feb 21, 2015 at 10:45 PM
var minutes = Mathf.Floor(time / 60);
Yup, that was what it was, thanks Jessespike. now I suppose all this timer needs is an hours slot?
Follow this Question
Related Questions
Unity 5 - Time counter Up script (millisecond precision) UI 1 Answer
Stop and Pause Timer 2 Answers
timer not ticking down 2 Answers
CountDownTimer Help 1 Answer
How to make this display milliseconds? 3 Answers