- Home /
Too subjective and argumentative
Networking Buttons and Syncvar
Hello everybody,
I'm developing a turn based game. My question is very simple. I have a button (currently as a scene object and canvas its attached to has network identity) and this button has a scrip attached. When player clicks on it, it generates random number like rolling dice. I'm using syncvar but I cannot make player objects to get this random number generated.
Below is my code attached to UI button as a part of scene;
public class Roll_Dice : NetworkBehaviour
{
[SyncVar] public int newDice_Score;
public int Dice_Score;
public Text Dice_Text;
public GameObject BoardManager;
public Button RollDice;
public bool PlayerTurn = false;
void Start()
{
RollDice.gameObject.SetActive(true);
Dice_Text.text = " ";
BoardManager.gameObject.SetActive(true);
Button btn1 = RollDice.GetComponent<Button>();
btn1.onClick.AddListener(TaskOnClick);
}
public void TaskOnClick()
{
Dice_Score = Random.Range(2, 13);
CmdDiceScore(Dice_Score);
Dice_Text.gameObject.SetActive(true);
Dice_Text.text = "Rolled" + " " + newDice_Score;
Invoke("Close_Dice_Roll", 3);
}
[Command]
void CmdDiceScore(int Dice_Score)
{
newDice_Score = Dice_Score;
RpcDiceScore(Dice_Score);
}
[ClientRpc]
void RpcDiceScore(int Dice_Score)
{
newDice_Score = Dice_Score;
Debug.Log("RPC");
}
public void Close_Dice_Roll()
{
Dice_Text.gameObject.SetActive(false);
RollDice.gameObject.SetActive(true);
PlayerTurn = !PlayerTurn;
}
}
And, below is attached to player object.
public class Roll_DicePlayer : NetworkBehaviour {
public Text Dice_Text;
public GameObject PlayerCanvasClone;
[SyncVar] public int newDice_Score;
public GameObject Dice_Button;
public override void OnStartLocalPlayer()
{
}
Answer by myzzie · Sep 03, 2018 at 07:52 PM
You don't need a syncvar if you're going to send it through an rpc and vice versa. With the syncvar, you only have to change the variable and the client's same variable will change.
Your Roll_DicePlayer class has nothing to do with the above code, not sure why you put it there.
Still Syncvar value doesnt update on player object;
Below are Syncvar values in Dice_Button and PlayerConnection object.