Trying to sync game currency between players but have problems to get it to work properly.
Here is the code:
[SyncVar] private int redValue = 10000;
[SyncVar] private int blueValue = 20000;
private int redFundsX = 10000;
private int blueFundsX = 20000;
void Update () {
if (redValue != redFundsX || blueValue != blueFundsX) {
print ("redValue: " + redValue + " != redFundsX: " + redFundsX + " || blueValue: " + blueValue + " != blueFundsX: " + blueFundsX);
print ("HIT");
DoPlayer (redFundsX, blueFundsX);
txt_RedTotalFunds.text = "$" + redValue.ToString();
txt_BlueTotalFunds.text = "$" + blueValue.ToString();
redFundsX = redValue;
blueFundsX = blueValue;
print ("blueFundsX: " + blueFundsX + " blueValue: " + blueValue);
}
}
[Client]
void DoPlayer (int _red, int _blue) {
if (hasAuthority) {
print ("Authority");
Cmd_Update (_red, _blue);
}
}
[Command]
void Cmd_Update (int _red, int _blue) {
if (redValue != _red) {
redValue = _red;
}
if (blueValue != _blue) {
blueValue = _blue;
}
}
void Btn_RED_test () {
if (!isLocalPlayer)
return;
print ("RED: " + gameObject.tag);
redFundsX = 1234;
}
void Btn_BLUE_test () {
if (!isLocalPlayer)
return;
print ("BLUE: " + gameObject.tag);
blueFundsX = 5678;
}
Basically what I am trying to do is that when click one of the buttons I change redFundsX and blueFundsX, which trigger the if-statements in Update.
It works with red and sync between the players. However, when I click on blue button I get problems.
result from print:
It loops between the two above and I cannot really figure out how to fix this.
Would really appreciate help.