Search Unity

Color variable not synchronizing across network

Discussion in 'Multiplayer' started by ghostravenstorm, Apr 26, 2017.

  1. ghostravenstorm

    ghostravenstorm

    Joined:
    Oct 18, 2015
    Posts:
    88
    I have a multiplayer game where clients connect and they generate a random color for themselves. I'm having problems getting the color value generated to get push to the server so that the server can push it to clients.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using UnityEngine.Networking;
    4.  
    5. public class TankIdentity : NetworkBehaviour{
    6.  
    7.    private System.Random rand;
    8.    [SyncVar] private Color color;
    9.  
    10.    void Awake(){
    11.       rand = new System.Random();
    12.    }
    13.  
    14.    public override void OnStartLocalPlayer(){
    15.       GenerateRandColor();
    16.    }
    17.  
    18.    public void GenerateRandColor(){
    19.       byte r = (byte)rand.Next(0, 255);
    20.       byte g = (byte)rand.Next(0, 255);
    21.       byte b = (byte)rand.Next(0, 255);
    22.  
    23.       color = new Color32(r, g, b, 255);
    24.  
    25.       CmdIterateChildren_(gameObject);
    26.  
    27.    }
    28.  
    29.    [Command]
    30.    private void CmdIterateChildren_(GameObject body){
    31.       if(body.transform.childCount == 0){
    32.          if(body.transform.GetComponent<MeshRenderer>() != null){
    33.             body.transform.GetComponent<MeshRenderer>().material.color = this.color;
    34.          }
    35.          return;
    36.       }
    37.  
    38.       for(int i = 0; i < body.transform.childCount; i++){
    39.          if(body.transform.GetComponent<MeshRenderer>() != null){
    40.             body.transform.GetComponent<MeshRenderer>().material.color = this.color;
    41.          }
    42.          CmdIterateChildren_(body.transform.GetChild(i).gameObject);
    43.       }
    44.    }
    45. }
    46.  
     
  2. ghostravenstorm

    ghostravenstorm

    Joined:
    Oct 18, 2015
    Posts:
    88
    Still can't get this working


    Code (CSharp):
    1. [SyncVar]
    2.    private Color color;
    3.  
    4.    void Start(){
    5.  
    6.       if(isLocalPlayer){
    7.          float r = Random.Range(0.0f, 1.0f);
    8.          float g = Random.Range(0.0f, 1.0f);
    9.          float b = Random.Range(0.0f, 1.0f);
    10.  
    11.          this.color = new Color(r, b, g, 1.0f);
    12.       }
    13.  
    14.       foreach(MeshRenderer mesh in GetComponentsInChildren<MeshRenderer>()){
    15.          mesh.material.color = this.color;
    16.       }
    17.    }
    Other players are either totally different colors or black.
     
  3. ghostravenstorm

    ghostravenstorm

    Joined:
    Oct 18, 2015
    Posts:
    88
    SyncVar automatically makes the server call to update other clients, right? So if a client sets their [SyncVar] Color color to something then it shuold get pushed to the server and updated for all teh clients, right?
     
  4. Zullar

    Zullar

    Joined:
    May 21, 2013
    Posts:
    651
    SyncVars only push from Server --> Client, not from Client --> Server

    The only 2 ways to send data from Client --> Server are
    1: [Command]
    2: MessageBase