Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

[SOLVED] What is wrong with this SyncVar

Discussion in 'Multiplayer' started by pKallv, Jun 28, 2017.

  1. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,194
    I am trying to use SyncVar but I do not fully understand what I am doing wrong, if I do it wrong that is. Here is the situation:

    1) I have two public SyncVar's: redFunds and blueFunds and two local "old"-version to compare with
    2) I initiate the SyncVar in Start by using Cmd_UpdateXxx, that works
    3) I have two buttons, one for each SyncVar
    4) In Update i compare the SyncVar vs. the oldXxxFunds. If a hit i display on scene

    When i run the code it does display the correct numbers on the scen on both players (Red & Blue) but that is not fully reflected in the editor when looking at the "public" SyncVar's. When pressing the red button it only reflect, in the editor, on the red player not the Blue.

    Can someone explain to me what i am doing wrong here? ...if I am doing something wrong that is. Shouldn't i see the changes in the editor as well?

    Code (CSharp):
    1. [SyncVar] public int redFunds;
    2. [SyncVar] public int blueFunds;
    3. public int oldRedFunds;
    4. public int oldBlueFunds;
    5.  
    6.  
    7.  
    8. void Start () {
    9.  
    10.     if (!isLocalPlayer)
    11.             return;
    12.     Cmd_UpdateRed (10);
    13.     Cmd_UpdateBlue (20);
    14.  
    15. }
    16.  
    17.  
    18. // Button
    19. void btn_Red () {
    20.     if (!hasAuthority)
    21.             return;
    22.     Cmd_UpdateRed (10000);
    23. }
    24.  
    25. void btn_Blue () {
    26.      if (!hasAuthority)
    27.             return;
    28.     Cmd_UpdateBlue (20000);
    29. }
    30.  
    31. [Command]
    32. void Cmd_UpdateRed (int _value) {
    33.     redFunds = _value;
    34. }
    35.  
    36. [Command]
    37. void Cmd_UpdateBlue (int _value) {
    38.     blueFunds = _value;
    39. }
    40.  
    41. void Update () {
    42.    
    43.     if (redFunds != oldRedFunds) {
    44.  
    45.         txt_RedTotalFunds = GameObject.Find ("txt_RedTotalFunds").GetComponent<Text> ();
    46.         txt_RedTotalFunds.text = "$" + redFunds;
    47.         oldRedFunds = redFunds;
    48.     }
    49.  
    50.     if (blueFunds != oldBlueFunds) {
    51.  
    52.         txt_BlueTotalFunds = GameObject.Find ("txt_BlueTotalFunds").GetComponent<Text> ();
    53.         txt_BlueTotalFunds.text = "$" + blueFunds;
    54.         oldBlueFunds = blueFunds;
    55.     }
    56.    
    57. }
    58.  
     
  2. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,194
    I solved the problem. The red and blue funds variables are unique in each player, so if you update the red funds in the red player, it will not be updated in the blue player. The solution is to have a, non player, script that is shared in a ScriptContainer with the actual SyncVar's, which is access from the players.