I’ve got this script that it seems to me should work once I figure out what I’m missing. It’s a 3-part timer (years, months, days - not counting up in real time obviously, so Time.time doesn’t really help). Anyone want to check out this code and see if you can figure it out?
#pragma strict
var yearCounter : int;
var monthCounter : int;
var dayCounter : int;
var clockSkin : GUISkin;
var clockStart = true;
function Start () {
}
function Update () {
}
function OnGUI () {
GUI.skin = clockSkin;
if (clockStart)
dayCounter ++;
// add +1 to dayCounter every 30 seconds - This is the part I'm trying to figure out
GUI.Label (Rect (100, 100, 50, 30), yearCounter.ToString());
GUI.Label (Rect (150, 100, 50, 30), monthCounter.ToString());
if (monthCounter >12) {
monthCounter = 0;
yearCounter ++;
}
GUI.Label (Rect (200, 100, 50, 30), dayCounter.ToString());
if (dayCounter >30) {
dayCounter = 0;
monthCounter ++;
}
}
Well, I prefer the Coroutine because you have more control. You can add stuff to start, stop and cancel it. You can archive the same using Invoke, but it will still be called.
SniperFan - Getting a compiler error on yours -Line 7, insert semicolon at the end. Clearly there is a semicolon… maybe I screwed something else up? Here’s the code as I have it…
IEnumerator Tick() {
while (true) {
yield return new WaitForSeconds(30f);
dayCounter++;
}
}
Novashot - yours “works”, but the dayCounter is rocketing up so fast as to make it unreadable - I think maybe it’s adding to the dayCounter every frame?
If its counting that fast you dont have it as I do above. Make sure the invoke call is in start and not in update also make sure that the arguments for invoke are correct