- Home /
This question was
closed Mar 05, 2018 at 05:01 AM by
Vencarii for the following reason:
Other
Question by
Vencarii · Mar 04, 2018 at 10:47 PM ·
networkingvariablelong
SyncVar of type long isn't updated on clients
Hello, I have a SyncVar called "money" of type long in my Player gameobject with a hook to the OnMoneyChange() method. This method updates a Text Gameobject to show the amount of money to the player. It works fine on the Host, but it doesn't on the Client.
I used Debug.Log("money="+money); to show the amount of money on the Client when the player moves. It shows 30000 as expected. But when I buy something in my game, the amount is still at 30000, so it doesn't sync with the server. And I don't see any of the debug messages in the OnMoneyChange() method.
Code on my player gameobject:
public class Player : NetworkBehaviour
{
[SyncVar(hook = "OnMoneyChange")]
public long money = 0;
void Start()
{
// init stuff for the local player
if (isLocalPlayer) {
gameObject.tag = "LocalPlayer";
targetPosition = transform.position;
money = 30000;
}
}
// hook that updates the money text
public void OnMoneyChange(long newMoney)
{
Debug.Log("OnMoneyChange called");
if (!isLocalPlayer) {
return;
}
money = newMoney;
Debug.Log(GameObject.FindGameObjectWithTag("MoneyText"));
GameObject.FindGameObjectWithTag("MoneyText").GetComponent<Text>().text = money.ToString("###,###,###") + " $";
Debug.Log("OnMoneyChange done");
}
// much more....
}
Comment