1. We've introduced thread tags, search within a thread and similar thread search. Read more here.
    Dismiss Notice
  2. Learn how you'll soon be able to publish your games to China in four simple steps with Xiaomi. Sign up now for early access.
    Dismiss Notice
  3. Get further faster with the Unity Plus Accelerator Pack, free for new Unity Plus subscribers for a limited time. Click here for more details.
    Dismiss Notice
  4. We've released our first Timeline Experimental Preview, our new tool for creating cutscenes and more! To check it out click here.
    Dismiss Notice
  5. Unity 5.5 is now released.
    Dismiss Notice
  6. Check out all the fixes for 5.5 in patch releases 1 & 2.
    Dismiss Notice
  7. Unity 5.6 beta is now available for download.
    Dismiss Notice

Unity Multiplayer Synchronizing Colors

Discussion in 'Multiplayer Networking' started by seanr, Jun 20, 2015.

  1. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    A Code snippet to allow synchronization of colors from the server to clients


    Code (CSharp):
    1.  
    2. class ColorSync : NetworkBehaviour
    3. {
    4.     [SyncVar(hook="OnColor")]
    5.     public Color myColor;
    6.  
    7.     void OnColor(Color newColor)
    8.     {
    9.         GetComponent<Renderer>().material.color = newColor;
    10.         myColor = newColor;
    11.     }
    12.  
    13.     public override void OnStartClient()
    14.     {
    15.         GetComponent<Renderer>().material.color = myColor;
    16.     }
    17. }
    18.  
     
    voltage, filip_andry, pKallv and 3 others like this.
  2. wicked208

    wicked208

    Joined:
    Oct 25, 2010
    Posts:
    83
    i tried the code but doesn work for me, server object dont update color in other clients
     
  3. Oshroth

    Oshroth

    Joined:
    Apr 28, 2014
    Posts:
    99
    It looks like you need an object on the server to set myColor for each client.
    Code (CSharp):
    1.  
    2. //In a NetworkManager-derived script
    3.     public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId) {
    4.         base.OnServerAddPlayer(conn, playerControllerId);
    5.         GameObject player = null;
    6.         PlayerController playerController = null;
    7.         if (conn.GetPlayerController(playerControllerId, out playerController)) {
    8.             player = playerController.gameObject;
    9.         }
    10.         if (player != null) {
    11.  
    12.             var playersyncer = player.GetComponent<ColorSync>();
    13.             playersyncer.myColor = RandomEx.RandomColor();
    14.  
    15.         }
    16.  
    17.     }