When a player is created he gets a random color, then I want all other players to sync with his color.
I can’t get the network to sync the players colors! Either only one gets synced, no one gets synced or every player sees a random color on every player.
How do I sync the color between all player?
I solved it myself, if anyone else is having trouble with this here it is
[SyncVar]private Color randomColor;
void Start(){
if(isLocalPlayer){
randomColor = new Color (Random.Range (0.0f, 1.0f), Random.Range (0.0f, 1.0f), Random.Range (0.0f, 1.0f));
GetComponentInChildren<Renderer>().material.color = randomColor;
}
}
[Command]
void Cmd_ProvideColorToServer(Color c){
randomColor = c;
}
[ClientCallback]
void TransmitColor(){
if(isLocalPlayer){
Cmd_ProvideColorToServer(randomColor);
}
}
public override void OnStartClient ()
{
StartCoroutine (UpdateColor (1.5f));
}
IEnumerator UpdateColor(float time){
float timer = time;
while (timer > 0) {
timer -= Time.deltaTime;
TransmitColor ();
if(!isLocalPlayer)
GetComponentInChildren<Renderer>().material.color = randomColor;
yield return null;
}
}
I really feel like this isn’t the best way to do it, I just kind of guessed away and eventually got the results I wanted. I only update the color for a short time after OnStartClient() because the color wont update if I just run it once in the OnStartClient() for some reason. Even though this works for me I would like to get some tips on how to improve this!