Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Can't get syncvars to work

Discussion in 'Multiplayer' started by Smikis, Mar 29, 2016.

  1. Smikis

    Smikis

    Joined:
    May 3, 2015
    Posts:
    7
    What I originally wanted is to have one main script/controller that would hold values that required by everyone else, couldn't figure out how to do it, so just tried to contain variables in each object and sync them. But even that fails...

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4.  
    5. public class Player : NetworkBehaviour
    6. {
    7.  
    8.    
    9.     Color[] colour = new Color[4] { Color.red, Color.yellow, Color.green, Color.blue };
    10.     [SyncVar(hook ="OnNr")]
    11.     private int nrOfPlayer = 0;
    12.  
    13.     [SyncVar(hook = "OnColor")]
    14.     private Color myColor;
    15.  
    16.     void OnColor(Color newColor)
    17.     {
    18.         GetComponent<Renderer>().material.color = newColor;
    19.         myColor = newColor;
    20.     }
    21.     void  OnNr(int number)
    22.     {
    23.         nrOfPlayer = number;
    24.         Debug.Log(number);
    25.     }
    26.  
    27.     public override void OnStartLocalPlayer()
    28.     {
    29.         //Debug.Log(nrOfPlayer);
    30.         myColor = colour[nrOfPlayer];
    31.         Renderer rend = GetComponent<Renderer>();
    32.         rend.material.shader = Shader.Find("Sprites/Default");
    33.         rend.material.color = myColor;
    34.         CmdPlayerNr();
    35.     }
    36.  
    37.     [Command]
    38.     void CmdPlayerNr()
    39.     {
    40.         nrOfPlayer++;
    41.         Debug.Log("cmd"+nrOfPlayer);
    42.     }
    43.  
    44. }
    45.  
    What I am trying to achieve is to have different colour for each player ( I cant sync colours as well), but before that I cant even seem to sync number of players I have, its always on 1, code is full or random rubbish at this point since I was trying everything I could imagine. There doesn't seem to be good explanation how syncvars work either, default documentation is sorely lacking.
    Tutorial one doesn't go into depth and from tutorial it looks simple, but doesn't work for me..
     
  2. srylain

    srylain

    Joined:
    Sep 5, 2013
    Posts:
    159
    Your hook functions I believe need to have "value" as their parameter, so like this:

    1. void OnNr(int value)
    2. {
    3. nrOfPlayer = value;
    4. Debug.Log(number);
    5. }
      1. void OnColor(Color value)
      2. {
      3. GetComponent<Renderer>().material.color = value;
      4. myColor = value;
      5. }
    I think that's the only problem I see. Normally your hook functions would still get called, but the values you would change your variables to would be default values.
     
  3. Smikis

    Smikis

    Joined:
    May 3, 2015
    Posts:
    7
    seems like strange parameter name requirement, but no that doesn't fix it :/
     
  4. srylain

    srylain

    Joined:
    Sep 5, 2013
    Posts:
    159
    SyncVars might also have to be set by the server. If they're anything like SyncListStructs, only when the server modifies them are they synced across all clients.
     
  5. Oshroth

    Oshroth

    Joined:
    Apr 28, 2014
    Posts:
    99
    If this is a script that is on every player, then it would always be 1 since everyone has their own copy of the variable. Syncvar Hook functions don't have a parameter name requirement.