Reputation: 51
I have been working on a Unity project for sometime, I have a timer which currently counts down from around 30 minutes, however I want this to be 4 hours counting down. here is my code:
seconds = Mathf.CeilToInt (gameTimer - Time.timeSinceLevelLoad) % 60;
minutes = Mathf.CeilToInt (gameTimer - Time.timeSinceLevelLoad) / 60;
}
if (seconds == 0 && minutes == 0) {
chestfree.gameObject.SetActive(true);
checker = 2;
}
remainingTime = string.Format("{0:00} : {1:00}", minutes, seconds);
if (seconds != 0 && minutes != 0) {
h.text = remainingTime.ToString ();
} else {
h.text = "Get";
}
So you can see I have seconds and minutes in there, I tried to change the / 60 after minutes to 19 and when it ran it displayed 240 minutes which was fine, but the countdown seems to be very odd, minutes go down after around 20 seconds so I know thats not the right way to do it!
Can anyone explain to me how to add in hours or how I can make the minutes count down from 240 minutes?
Many thanks!
Upvotes: 0
Views: 1766
Reputation: 189
Untested code below:
hours = Mathf.FloorToInt((float)(gameTimer - Time.timeSinceLevelLoad) / 3600);
minutes = Mathf.FloorToInt((float)(gameTimer - Time.timeSinceLevelLoad-hours*3600) / 60);
seconds = Mathf.FloorToInt((gameTimer - Time.timeSinceLevelLoad) % 60);
if (seconds == 0 && minutes == 0 && hours == 0) {
chestfree.gameObject.SetActive(true);
checker = 2;
}
//remainingTime = string.Format("{0}:{1}:{2}",hours,minutes,seconds);
remainingTime = string.Format("{2:00} : {0:00} : {1:00}", minutes, seconds,hours);
if (seconds != 0 && minutes != 0 && hours != 0) {
h.text = remainingTime.ToString ();
} else {
h.text = "Get";
}
I'm not sure about the construction of the remainingTime string. I would have used something like this:
remainingTime = string.Format("{0}:{1}:{2}",hours,minutes,seconds);
EDIT:
(gameTimer - Time.timeSinceLevelLoad)/60
is an integer operation, that's why it did not work. By forcing float operation it should work.
Upvotes: 0