Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

NetworkVariable - variable not updating correct

Discussion in 'Multiplayer' started by simondegn, Jul 3, 2022.

  1. simondegn


    Oct 26, 2017
    I am building a turn based game with Unity and Netcode.

    My connections work and I receive expected behavior on the NetworkManager.

    Except for one thing...

    I have a server variable, that should be aligned on all clients. It's the index of the player (turn) in the game (CurrentPlayerIndex). The turn should change after a action has been done, but for testing I just made it a button action, so that all clients can change turn (change the CurrentPlayerIndex).

    I made it work, so that CurrentPlayerIndex changes correctly on all clients, when one client change the CurrentPlayerIndex. It works so that the variable in a visual label updates with the same variable on all clients.

    The problem is that the variable doesn't get stored on the client, so when I increase the CurrentPlayerIndex on one client, it shows the correctly increased CurrentPlayerIndex in the label BUT when I increase the same (already increased) variable on the same client, it just starts from the local index instead.

    Let me explain...

    I have two clients. Client1 and Client2. CurrentPlayerIndex starts on 0. When I click "increase" three times on Client1, CurrentPlayerIndex increases to 3. This is displayed on both Client1 and Client2. But when I click "increase" 2 times on Client2, the CurrentPlayerIndex starts from 0 again and increases to 3. When I click "increase" on Client1 again, the CurrentPlayerIndex increases to 4 and so on...

    I expect that I misunderstand something about the local client and server behavior, but in this case I just want a "global" variable, that can be affected by all clients and aligned on all clients.

    Here's the code snippet:

    public NetworkVariable<int> CurrentPlayerIndex = new NetworkVariable<int>();

    public override void OnNetworkSpawn() {
    CurrentPlayerIndex.OnValueChanged += OnCurrentPlayerIndexChanged;

    public override void OnNetworkDespawn() {
    CurrentPlayerIndex.OnValueChanged -= OnCurrentPlayerIndexChanged;

    public void OnCurrentPlayerIndexChanged(int previous, int current) {
    UIManager.Instance.gameMenuLabel.text = "Player " + CurrentPlayerIndex.Value + "'s turn";

    [ServerRpc(RequireOwnership = false)]
    public void ChangeTurnServerRpc(int nextPlayerIndex) {
    CurrentPlayerIndex.Value = nextPlayerIndex;

    public void ChangeTurn() {

    int nextPlayerIndex = 0;

    if (CurrentPlayerIndex.Value+1 < 5) {
    nextPlayerIndex = CurrentPlayerIndex.Value+1;

  2. cerestorm


    Apr 16, 2020
    I don't know what the issue is but I would suggest a different approach. Have another network object like Turn that contains the CurrentPlayerIndex and have the player send an End Turn message to the server and then it can set the next player index.

    You'll probably want to expand the Turn object later if you want to add a turn number or a countdown timer.
  3. totobono4


    Mar 14, 2018
    Same Issue here, the thing is that the network variable is not updated when the callback OnValueChanged is called, I don't know why this happen, you have to use the int current to makes this work.