Thanks for your reply
İ try to explain : if i join the game from host it starts at 00.00 am and let’s say i play until it’s 4 am.
But now if the client joins it starts for him at 00.00 am again but for the host its 4 am.
İ mean simply it’s not synced over UNet.
Okay, I thought that’s what you might have meant. From solely the portion of code that you posted, there is no communication to clients that the time has changed. The host observes the change, but no update is communicated. Gotta tell them the time when they connect (and adjust the day/night time) based on that.
And exactly this point is where İ don’t know what to do :'D
İ read much about the [command] function and i did tests with other scripts like health/damage but i couldn’t use that on this script it’s too complicated somehow
Okay, did your health / damage tests work as expected? If so, I would suggest that you allow everyone to run the ChangeTime() script (not just the server), however… have a SetDayTime() (or something like that) script that only clients run. When they join, the server sends them the time variable, which the client saves as their own local copy which is used to update regularly. This way things will be as close as you can get, I think.
If you’re at 4am, and a client connects, you tell them it’s 4am… you each run your separate functions to update the time of day… you needn’t keep syncing the time back n forth for that session, in my opinion. Does that make any sense?
Ok, I don’t know why it was that easy but i found the Solution. It seems like i wasn’t even used to use rpc’s I just put a Network view and a Network Transform script on to it and changed the script some. Thank you very much you saved me <3
for everyone that’s maybe having the same issue here’s my script :
using System.Collections;
using System;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
public class DayNightCycle : NetworkBehaviour {
//[SerializeField]
[SyncVar]public float time;
public TimeSpan currenttime;
[SerializeField] public Transform SunTransform;
public Light Sun;
public Text timetext;
[SyncVar]public int days;
[SyncVar]public float intensity;
[SyncVar]public int speed;
// Update is called once per frame
void Update ()
{
if(isLocalPlayer) // Just changed this for localplayer
return;
ChangeTime();
}
public void ChangeTime()
{
//if(isServer){ // and I just closed this line
time += Time.deltaTime * speed;
if(time > 86400) //86400 = 00:00
{
days += 1;
time = 0;
}
currenttime = TimeSpan.FromSeconds (time);
string[] temptime = currenttime.ToString () .Split (":"[0]);
timetext.text = temptime[0] + ":" + temptime[1];
SunTransform.rotation = Quaternion.Euler (new Vector3((time-21600)/86400*360,0,0));
if (time < 43200)
intensity = 1 - (43200 - time) / 43200;
else //Even if i disable this lines there's no changing in the darkness of the game...
intensity = 1 - ((43200 - time) / 43200 *-1);
Sun.intensity = intensity;
//}
}
}