Search Unity

  1. We have added sub-forums for the experimental MLAPI and Unity Transport Package. Please use the new forums to discuss related topics.
    Dismiss Notice
  2. All Pro and Enterprise subscribers: find helpful & inspiring creative, tech, and business know-how in the new Unity Success Hub. Sign in to stay up to date.
    Dismiss Notice
  3. Dismiss Notice

Subscribing To NetworkVariable Change Calling Twice On Client

Discussion in 'MLAPI' 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

    Unity Technologies

    Joined:
    Sep 30, 2020
    Posts:
    126
    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
     
unityunity