Search Unity

Subscribing To NetworkVariable Change Calling Twice On Client

Discussion in 'Netcode for GameObjects' started by RobertGrospitch, Jun 8, 2021.

  1. RobertGrospitch

    RobertGrospitch

    Joined:
    Sep 8, 2020
    Posts:
    7
    Hello All,

    I am looking to subscribe to a network variable change on both the client and the server. I watched the DapperDino guide and have it working, however, it seems that on the client the subscribe is happening twice, even when the old kills amount is equal to the new amount

    Current Subscribe:
    Code (CSharp):
    1. playerKillsDeaths.playerKills.OnValueChanged += OnChangeKills;
    Current OnChangeKills:
    Code (CSharp):
    1.     void OnChangeKills(int oldKills, int newKills)
    2.     {
    3.         Debug.Log($"old kills {oldKills} new kills {newKills}");
    4.     }
    The output on the server is correct and only prints once, however, the output on the client is running twice and printing old and new as the same after it prints the update as seen below.
    upload_2021-6-8_16-10-23.png

    I am correctly getting the script playerKillsDeaths from the current PlayerObject and ensuring its the localplayer by trying to use "IsLocalPlayer" and also tested "IsOwner".

    The subscribe is also occurring in the OnAwake on the player.

    I am doing math within this OnChangeKills() method and running twice will cause issues. Is this how OnValueChange is intended to work?

    Thank you,
     
  2. luke-unity

    luke-unity

    Joined:
    Sep 30, 2020
    Posts:
    306
    This is a known issue in MLAPI currently. If a client updates a NetworkVariable the OnValueChanged event gets called twice.
     
  3. RobertGrospitch

    RobertGrospitch

    Joined:
    Sep 8, 2020
    Posts:
    7
    Thank you