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

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:

   public override void OnNetworkSpawn()
   {
       if (!IsOwner) //called on client
       {
           //called on client right after spawning on 1.0.2 (never called on 1.1.0)
           networkInit.OnValueChanged += UpdateBulletInit;
       }
       else //called on server/owner
       {
           //NetworkVariable value changed in server,
           //makes OnValueChanged being called instantly on client
           networkInit.Value = new BulletNetworkInit(){ _initialForce = 1, };
       }
   }

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:

    public override void OnNetworkSpawn()
    {
        if (!IsOwner) //called on client
        {
            //call the initialization manually
            UpdateBulletInit(networkInit.Value, networkInit.Value);
        }
        else //called on server/owner
        {
            //NetworkVariable value changed in server,
            //makes OnValueChanged being called instantly on client
            networkInit.Value = new BulletNetworkInit(){ _initialForce = 1, };
        }
    }

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: NetworkVariable OnValueChanged() not being called on spawn · Issue #2269 · Unity-Technologies/com.unity.netcode.gameobjects · GitHub

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.

1 Like

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.

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.

What unity version do you need in order to see 1.1.0 on the package manager and should I move to it?

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).

Thank you!
I also have this problem in the new version in network list

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