Clock is counting by twos?

Yeah, I… what?

This code is on a server-instantiated object, so that client-side objects can use the time on the server proper. But for some reason, when it prints every second (like it’s supposed to), it goes up by two seconds rather than the one (like it’s NOT supposed to.)

using UnityEngine;
using System.Collections;

public class Timekeeper : MonoBehaviour {

	int hours;
	int minutes;
	int seconds;
	int secondOfDay;

	public float speed;
	public bool syncWithSystemTime;

	void Start (){
		if(syncWithSystemTime){
			secondOfDay = (System.DateTime.Now.Hour * 3600) + (System.DateTime.Now.Minute * 60) + (System.DateTime.Now.Second);
		}
		InvokeRepeating ("CountUp", 0, speed);
	}

	void CountUp (){
		if(secondOfDay < 86400){
			secondOfDay++;
		}
		else{
			secondOfDay = 0;
		}

		hours = secondOfDay / 3600;
		minutes = (secondOfDay / 60) - (hours * 60);
		seconds = secondOfDay - (minutes * 60) - (hours*3600);

		networkView.RPC("syncTime", RPCMode.OthersBuffered, hours, minutes, seconds, secondOfDay);
	}

	[RPC]
	void syncTime (int newHour, int newMinute, int newSecond, int newSOD){
		hours = newHour;
		minutes = newMinute;
		seconds = newSecond;
		secondOfDay = newSOD;

		print ("Server Time: " + (hours.ToString()) + ":" + (minutes.ToString()) + ":" + (seconds.ToString()) + ";   " + secondOfDay.ToString());
	}
}

There’s an issue with InvokeRepeating if you start with 0, so try some small number that’s not 0 instead. By the way, variables are automatically converted to strings when doing string concatenation, so you can do away with all the ToString()s. "Server Time: " + hours + “:” + minutes …

–Eric

Sweet, thanks for quick reply :smile:

Unfortunately, it’s still going by twos, even with the one-second delay.

using UnityEngine;
using System.Collections;

public class Timekeeper : MonoBehaviour {

	int hours;
	int minutes;
	int seconds;
	int secondOfDay;

	public float speed;
	public bool syncWithSystemTime;

	void Start (){
		if(syncWithSystemTime){
			secondOfDay = (System.DateTime.Now.Hour * 3600) + (System.DateTime.Now.Minute * 60) + (System.DateTime.Now.Second);
		}
		InvokeRepeating ("CountUp", 1, speed);
	}

	void CountUp (){
		if(secondOfDay < 86400){
			secondOfDay++;
		}
		else{
			secondOfDay = 0;
		}

		hours = secondOfDay / 3600;
		minutes = (secondOfDay / 60) - (hours * 60);
		seconds = secondOfDay - (minutes * 60) - (hours*3600);

		networkView.RPC("syncTime", RPCMode.OthersBuffered, hours, minutes, seconds, secondOfDay);
	}

	[RPC]
	void syncTime (int newHour, int newMinute, int newSecond, int newSOD){
		hours = newHour;
		minutes = newMinute;
		seconds = newSecond;
		secondOfDay = newSOD;

		print ("Server Time: " + hours + ":" + minutes + ":" + seconds + ";   " + secondOfDay);
	}
}

Found the solution. If I moved the mathy bits into the RPC and adjusted line 31 appropriately, in other words making client-side do the math, while still being tethered to the server-side secondOfDay variable, it increments properly.

using UnityEngine;
using System.Collections;

public class Timekeeper : MonoBehaviour {

	int hours;
	int minutes;
	int seconds;
	int secondOfDay;

	public float speed;
	public bool syncWithSystemTime;

	void Start (){
		if(syncWithSystemTime){
			secondOfDay = (System.DateTime.Now.Hour * 3600) + (System.DateTime.Now.Minute * 60) + (System.DateTime.Now.Second);
		}
		InvokeRepeating("CountUp", 1, speed);
	}

	void CountUp (){
		if(secondOfDay < 86400){
			secondOfDay++;
		}
		else{
			secondOfDay = 0;
		}



		networkView.RPC("syncTime", RPCMode.OthersBuffered, secondOfDay);
	}

	[RPC]
	void syncTime (int sod){
		hours = sod / 3600;
		minutes = (sod / 60) - (hours * 60);
		seconds = sod - (minutes * 60) - (hours*3600);

		print ("Server Time: " + hours + ":" + minutes + ":" + seconds + ";   " + secondOfDay);
	}
}

A little off topic, but there are also easier ways to get things like “secondOfTheDay”. Not necessarily an efficiency issue but it does make it a little more obvious. Timespans can be your friend. Here’s an example:

var secondOfTheDay = (DateTime.Now - DateTime.Parse(DateTime.Now.ToShortDateString())).TotalSeconds;

Subtracting the date only portion from the current date returns a timespan.