Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on Dec 8, 2022, between 7 am & 7 pm EST, in the DOTS Dev Blitz Day 2022 - Q&A forum, Discord, and Unity3D Subreddit to learn more about DOTS directly from the Unity Developers.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Bug NetworkBehaviour NetworkVariable OnValueChanged bug or by design?

Discussion in 'Netcode for GameObjects' started by kakhao, Jul 18, 2021.

  1. kakhao

    kakhao

    Joined:
    Jan 1, 2020
    Posts:
    4
    Strange thing, NetworkVariable fires OnValueChanged twice only on client if it IsOwner.
    First time we have real update with diferent previous and new values and second time both parameters are the same. Is it bug?

    BTW, maybe for someone it would be useful, NetworkBehaviour behaviour :
    If host:
    1. Awake()
    2. OnEnable()
    3. NetworkStart()
    4. Start()
    On Clients Side we have
    1. Awake() for Remote NetworkObject;
    2. OnEnable() for Remote
    3. NetworkVariable OnValueChanged Remote if one registered before - we receive fresh value
    4. NetworkStart() for Remote
    5. Awake() for Local
    6. OnEnable for Local
    7. NetworkVariable OnValueChanged Local - we receive default value regardless no changes done before;
    8. NetworkStart() for Local
    9. Start for Remote
    10. Start for Local (here we set own NetworkVariable)
    11. NetworkVariable OnValueChanged Local - we receive updated value
    12. NetworkVariable OnValueChanged Local - we receive updated value again regardless no changes
     
  2. kakhao

    kakhao

    Joined:
    Jan 1, 2020
    Posts:
    4
    Discovered this case, seems we have scenario:
    1. Client sets the NetworkVariable and this way fires ValueChanged event;
    2. NetworkManager sends new value of the variable to server, server updates the variable and send to all clients;
    3. Clients call NetworkVariable.ReadDelta and now it doesn't check if variable changed, just fires event.
     
  3. luke-unity

    luke-unity

    Unity Technologies

    Joined:
    Sep 30, 2020
    Posts:
    306