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.

Bug NetworkVariable OnValueChanged() not being called on spawn [Netcode 1.1.0]

Discussion in 'Netcode for GameObjects' started by pradotech, Oct 22, 2022.

  1. pradotech

    pradotech

    Joined:
    Oct 17, 2019
    Posts:
    35
    My game was running fine with Netcode 1.0.2, but after I upgraded to 1.1.0 some issues started to occur. There is a short example of code that works fine in 1.0.2:
    Code (CSharp):
    1.  
    2.    public override void OnNetworkSpawn()
    3.    {
    4.        if (!IsOwner) //called on client
    5.        {
    6.            //called on client right after spawning on 1.0.2 (never called on 1.1.0)
    7.            networkInit.OnValueChanged += UpdateBulletInit;
    8.        }
    9.        else //called on server/owner
    10.        {
    11.            //NetworkVariable value changed in server,
    12.            //makes OnValueChanged being called instantly on client
    13.            networkInit.Value = new BulletNetworkInit(){ _initialForce = 1, };
    14.        }
    15.    }
    16.  
    If spawned on server, the NetworkVariable immediately has its value changed (initialized).
    If the Bullet was spawned on client, it would listen to every change of its NetworkVariable value on server.

    This works fine in 1.0.2 in the way that client always get OnValueChanged called right after its spawn and everything is initialized correctly on client-side.

    But after upgrading to Netcode 1.1.0 the OnValueChanged event is not beign called on client.

    Instead, I had to call the initialization method directly doing something like:
    Code (CSharp):
    1.     public override void OnNetworkSpawn()
    2.     {
    3.         if (!IsOwner) //called on client
    4.         {
    5.             //call the initialization manually
    6.             UpdateBulletInit(networkInit.Value, networkInit.Value);
    7.         }
    8.         else //called on server/owner
    9.         {
    10.             //NetworkVariable value changed in server,
    11.             //makes OnValueChanged being called instantly on client
    12.             networkInit.Value = new BulletNetworkInit(){ _initialForce = 1, };
    13.         }
    14.     }
    I don't know what is the best approach, but everything was working fine before. I'm afraid there's more changes to be made in the project to suit this new Netcode version.

    I'm going back to 1.0.2 for now, but I appreciate any feedback or tips regarding this issue.

    Issue opened: https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/issues/2269
     
  2. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    426
    I saw your Github post and tested this and did wonder if not calling OnValueChanged on spawning was an intended change. Looking at it again though OnValueChanged is still being called on the server/host so this looks like a bug. I'll mention it on your Github post.
     
    pradotech likes this.
  3. pradotech

    pradotech

    Joined:
    Oct 17, 2019
    Posts:
    35
    Good to know that it could really be a bug. Thanks.

    I also noticed that NetworkManager spawn player prefabs differently after this update. In 1.0.2 the prefabs are instantiated in its original transform. In 1.1.0 they are spawned in world center position.

    At least those are the only "broken" things that I could quickly found. My concern is that could be more issues in this update, so for now I think I'll stick with 1.0.2.
     
  4. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    426
    Thinking about it a little more, it may be intended behaviour. On the server the value is changed after spawining so it's correct to call OnValueChanged. On the client the object spawns with the already updated value so there's no need to trigger the OnValueChanged call. There's definitely been a change in behaviour though, it will be interesting to see what the Unity developers say about it.

    I'm using 1.1.0 in my main project with no ill effects so far, it seems pretty stable.
     
  5. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    99
    What unity version do you need in order to see 1.1.0 on the package manager and should I move to it?
     
  6. pradotech

    pradotech

    Joined:
    Oct 17, 2019
    Posts:
    35
    I don't know for sure, I'm using 2021.3.9f1 but I upgraded just by manually selecting "Add package by name..." on Package Manager, inserting "com.unity.netcode.gameobjects" in name and and "1.1.0" in version (without quotations marks).
     
  7. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    99
    Thank you!
    I also have this problem in the new version in network list
     
  8. Suduckgames

    Suduckgames

    Joined:
    Nov 28, 2016
    Posts:
    216
    I also have this problem with the NetworkList too.

    It seems like a bug more than an intended behaviour ( at least in the list).

    Because if the client connects lately it won't receive the callback of OnValueChanged however, when you add 1 more item to the list in the first client, the other clients will receive the OnValueChanged for all items in the list
     
    Last edited: Oct 25, 2022