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:
- I have two public SyncVar’s: redFunds and blueFunds and two local “old”-version to compare with
- I initiate the SyncVar in Start by using Cmd_UpdateXxx, that works
- I have two buttons, one for each SyncVar
- 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?
[SyncVar] public int redFunds;
[SyncVar] public int blueFunds;
public int oldRedFunds;
public int oldBlueFunds;
void Start () {
if (!isLocalPlayer)
return;
Cmd_UpdateRed (10);
Cmd_UpdateBlue (20);
}
// Button
void btn_Red () {
if (!hasAuthority)
return;
Cmd_UpdateRed (10000);
}
void btn_Blue () {
if (!hasAuthority)
return;
Cmd_UpdateBlue (20000);
}
[Command]
void Cmd_UpdateRed (int _value) {
redFunds = _value;
}
[Command]
void Cmd_UpdateBlue (int _value) {
blueFunds = _value;
}
void Update () {
if (redFunds != oldRedFunds) {
txt_RedTotalFunds = GameObject.Find ("txt_RedTotalFunds").GetComponent<Text> ();
txt_RedTotalFunds.text = "$" + redFunds;
oldRedFunds = redFunds;
}
if (blueFunds != oldBlueFunds) {
txt_BlueTotalFunds = GameObject.Find ("txt_BlueTotalFunds").GetComponent<Text> ();
txt_BlueTotalFunds.text = "$" + blueFunds;
oldBlueFunds = blueFunds;
}
}