- Home /
Digital Clock: Time.time > 60
I am trying to make a functioning digital clock and it works except for the fact that I cannot seem to get the minutes to reset after 60.
var textObject: GUIText;
private var mouse: boolean;
mouse = false;
private var count: int;
count = 1;
private var Minute = 60;
function OnMouseOver ()
{
mouse = true;
}
function OnMouseExit ()
{
mouse = false;
}
function Update () {
var hour: int;
hour = 0 + Time.time / 60;
var minute: int;
minute = 0 + Time.time;
if (Input.GetKeyDown("e") && mouse == true)
{
textObject.text = "The time is " + hour + ":" + minute;
}
if (Time.time > 59)
{
minute = minute -60;
}
}
I was wondering if anyone knew what was wrong with this code
Comment
Answer by robertbu · Feb 12, 2013 at 02:04 AM
Calculate the minutes like:
var minute: int = Time.time - hour * 60;
And remove the:
minute = minute - 60;
Note you will need to do some string padding or formatting or you will get times like 0:7.
Answer by numberkruncher · Feb 12, 2013 at 02:11 AM
Why not just use this approach:
private var startTime : System.DateTime;
function Awake() {
// Reset timer from start of scene.
startTime = System.DateTime.Now;
}
function OnUpdate() {
var duration : System.TimeSpan = System.DateTime.Now - startTime;
textObject.text = string.Format("The time is {0}:{1:d2}", duration.Hours, duration.Minutes);
}
I haven't tested the above, but this concept should work.