Clock Script - Almost working I think

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 ++;
		}
}

If you want it to go up by 1 every 30 seconds in real time, why not use Time.time to check when 30 seconds have passed?

something like my edits above should work.

Fyko-chan is right.

Use a Coroutine and WaitForSeconds.

private void Start() {
    StartCoroutine(Tick());
}

private IEnumerator Tick() {
        while (true) {
            yield return new WaitForSeconds(30f);
            dayCounter++;
        }
}

EDIT: Damn Ninjas. Ninjas everywhere. :smile:

I fear you underestimate my sneakiness sir…

Coroutine or invoke will get you what you need… I prefer Invoke… and yield to nothing…or something like that.

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.

Or can you stop it again? I’m not sure. :face_with_spiral_eyes:

CancelInvoke works to stop invoked functions.

Hmmmmm still not quite there…

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?

Must be every frame as it’s counting 3,240 days per 30 seconds. Needless to say this is a touch fast.

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

This is a complete cut n paste of the script as I have it…

#pragma strict

var yearCounter : int;
var monthCounter : int;
var dayCounter : int;
var clockSkin : GUISkin;
var clockStart = true;

function Start () {
	InvokeRepeating ("AddDay", 30.0, 30.0);
}

function AddDay(){
	dayCounter ++;
}

function OnGUI () {
	GUI.skin = clockSkin;
	if (clockStart)
		dayCounter ++;
	GUI.BeginGroup (new Rect (20, Screen.height-90, 150, 50));
		GUI.Box (Rect (0, 0, 35, 50), "Year");
			GUI.Label (Rect (0, 10, 35, 50), yearCounter.ToString());
		GUI.Box (Rect (35, 0, 40, 50), "Month");
			GUI.Label (Rect (35, 10, 40, 50), monthCounter.ToString());
				if (monthCounter >12) {
					monthCounter = 0;
					yearCounter ++;
					}
		GUI.Box (Rect (75, 0, 35, 50), "Day");
			GUI.Label (Rect (75, 10, 35, 50), dayCounter.ToString());
				if (dayCounter >30) {
					dayCounter = 0;
					monthCounter ++;
				}
	GUI.EndGroup ();
}

You are adding days in ongui… which can fire multiple times in one frame

Yep. Add that to the long list of really obvious stuff I should have noticed. Thanks.

The only remaining question would be - is there a way to make this adjustable? Meaning, say, have 3-4 bool variables, each setting a different speed?