Description
Feedback
When load scene with in-scene placed NetworkObject
or dynamically spawn NetworkObject
, connected client would spawn the object and NetworkVariable
value synced with the server. But the OnValueChanged
is not called, even if the value has been synced with the value of server, and is different from the default value that has been set in NetworkBehaviour
. The current behavior of skipping OnValueChanged
when spawning also doesn't depend on where the OnValueChanged
has been registered, in OnNetworkSpawn
, OnNetworkPreSpawn
or even in Awake
.
This caused a lot of problems, because NetworkVariable
may be a bool that enable/disable a renderer, or set a shader field through OnValueChanged
. Skipping OnValueChanged
would make render frame inconsistent with what's intended. This is specially annoying when late-join client starts to spawn all NetworkObject
, without OnValueChanged
the game states would drastically different from server. The problem also shows up when a previously NetworkHide
object become visible again and client respwan the object but with inconsistent states from server.
Suggested Changes
When spawning, all NetworkVariable
should be synchronized, and corresponding OnValueChanged
should all get called, because clients are supposed to keep the same states as server. The server has likely changed the value of NetworkVariable
and have already called the OnValueChanged
on server side, so when spawning happens, client need to not only sync all values of NetworkVariable
, but also call all OnValueChanged
to keep the same states as server.
An intuitive way is to register OnValueChanged
in OnNetworkPreSpawn
, so that during subsequent spawning any valid callbacks registered will get called.