I made a test project which you can clone on Github, and documented my issue here.
In essence the problem is that I have two timers; one that counts down from 70 seconds to 0 (and then starts all over again), and the second one just shows the current client time.
Problems which I have are the following:
- If you open up any other window you will notice that the timer will stop, and continue counting once you return to the Unity window
- TimerText and TimeText are not “ticking off” at the same time
If someone has hints on how to approach solving this, I would be very grateful.
For reference, my code is this:
#pragma strict
var timer: float = 70;
var isFinishedLevel : boolean = false;
public var displayText : UnityEngine.UI.Text;
public var timeText : UnityEngine.UI.Text;
private var oldTimer : float;
function Start(){
oldTimer = timer;
}
function Update()
{
if (!isFinishedLevel) {
timer -= Time.deltaTime;
}
if (timer > 0) {
var minsDisplay : String = parseInt( timer / 60 ).ToString();
var secsDisplay : String = parseInt( timer ).ToString();
if ( (timer - ( parseInt(minsDisplay) * 60)) > 10 ) {
secsDisplay = parseInt( timer - ( parseInt(minsDisplay) * 60) ).ToString();
}
else {
secsDisplay = "0" + parseInt( timer - ( parseInt(minsDisplay) * 60) ).ToString();
}
displayText.text = minsDisplay + " : " + secsDisplay;
}
else {
timer = oldTimer;
}
CurrentTime();
}
function CurrentTime() {
var dt : System.DateTime = System.DateTime.Now;
var h : int = dt.Hour;
var m : int = dt.Minute;
var s : int = dt.Second;
timeText.text = h + ":" + m + ":" + s;
}