- Home /
Question by
Madswint · May 21, 2015 at 07:19 PM ·
javascripttimertime.deltatimeifdeltatime
I cant get this timer to start when this bool turns true?
I cant see how the timer works with the startTime = Time.time; and var guiTime = Time.time - startTime;
I just dont understand that, so I dont know how to make the timer start when GameStarted is true, instead of starting when the game loads. I tried enabling the script when GameStarted turned true, but the timer starts when the game is started for some reason. Thanks in advance.
private var startTime;
private var restSeconds : int;
private var roundedRestSeconds : int;
private var displaySeconds : int;
private var displayMinutes : int;
var countDownSeconds : int;
function Awake() {
startTime = Time.time;
}
function Update() {
if (GetComponent("HS").GameStarted == true) {
// Start timer here
}
}
function OnGUI () { //make sure that your time is based on when this script was first called //instead of when your game started
var guiTime = Time.time - startTime;
restSeconds = countDownSeconds - (guiTime);
//display messages or whatever here -->do stuff based on your timer
if (restSeconds == 60) {
print ("One Minute Left");
}
if (restSeconds <= 0) {
restSeconds = 0;
print ("Time is Over");
GetComponent("HS").TimesUp = true;
GetComponent("HS").GameStarted = false;
}
//display the timer
roundedRestSeconds = Mathf.CeilToInt(restSeconds);
displaySeconds = roundedRestSeconds % 60;
displayMinutes = roundedRestSeconds / 60;
text = String.Format ("{0:00}:{1:00}", displayMinutes, displaySeconds);
GUI.Label (Rect (400, 25, 100, 30), text);
}
Comment
Answer by ZTheDragonZ · May 31, 2015 at 11:46 AM
When You say starttime = Time.time; in the awake function starttimer will = to the first number in that comes from Time.time try Debug.Log (startTTime) in update to understand it well
and you need to set it on update function