I have this timer script that I thought was working but then I noticed that it increases by 1 minute every 30 seconds. Can anyone help me spot the error?
private var startTime;
private var finalTime:float;
public var gameEnded:boolean;
var textTime : String;
var customSkin:GUISkin;
function Awake(){
startTime =Time.time;
}
function OnGUI(){
var guiTime;
if(!gameEnded)
guiTime = Time.time - startTime;
else
guiTime = finalTime;
var minutes : int = guiTime / 60;
var seconds : int = guiTime % 60;
var fraction : int = (guiTime * 100) % 100;
if(!gameEnded)
{
text = String.Format ("{0:00}:{1:00}:{2:00}", minutes, seconds, fraction);
this.guiText.text = text;
guiText.material.color = Color.black;
}
}
What exactly does it do? Do the seconds increase twice a second, or does it get to 30 seconds, and increase the minutes by one? Does it increase the minutes counter twice a minute? I suspect that what you are getting is a rounding issue from the integer division. I think it might be rounding the result up, meaning that the 'minutes' increases at the wrong time in the minute!
Thanks but I eventually found the answer that allowed me to keep the fractions. Here’s what needed to change from my original script, just in case you want a timer with fractions:
var minutes : int = Mathf.Floor(guiTime/59);
var seconds : int = Mathf.Floor (guiTime % 59);
var fraction : int = (guiTime * 100) % 100;
What exactly does it do? Do the seconds increase twice a second, or does it get to 30 seconds, and increase the minutes by one? Does it increase the minutes counter twice a minute? I suspect that what you are getting is a rounding issue from the integer division. I think it might be rounding the result up, meaning that the 'minutes' increases at the wrong time in the minute!
– syclamoth