#NetworkBehaviour inconsistency on non-Owners
1 messages · Page 1 of 1 (latest)
NetworkObject networkObject;
NetworkVariable<bool> readyToInitialize = new(writePerm: NetworkVariableWritePermission.Owner);
NetworkVariable<Color> playerColor = new(writePerm: NetworkVariableWritePermission.Owner);
public override void OnNetworkSpawn()
{
base.OnNetworkSpawn();
networkObject = GetComponent<NetworkObject>();
if (!networkObject.IsOwner)
{
return;
}
//set networkvariable values
}
void Update()
{
if (readyToInitialize.Value)
{
Initialize();
return;
}
if (!networkObject.IsOwner)
{
return;
}
if (MouseManager.Instance.IsGround(out Vector3? position))
{
transform.position = position.Value;
}
//other initializing logic
if(isReady)//for simplification purposes
{
readyToInitialize.Value = true;
}
}
void Initialize()
{
Debug.Log(networkObject.IsOwner ? "Owner " : "" + "Asset is initializing with " + playerColor.Value);
foreach (var r in renderers)
{
r.material.color = playerColor.Value;
}
}
host: left, orange color
client: right, blue(ish) color
I think this is an appropriate minimalist example:
- the
Initialize()method is always executed on the owner but sometimes not on the client - the screen shot shows the spawned assets and the corresponding logs, it can be seen that sometimes the "non owner" log is missing and therefore the color is not applied, best seen on the client screen since the transform of the "non owner" assets is synched, in this case one asset is gray because it was not initialized, but the first of the client assets was not initialized as well, the blue color is seen only due to overlap of the second asset.
In regards to transform i dont have any particular code for the transform synchronization, in one works on the other doesn't.. Note that rigidbody is destroyed after spawn its only to detect overlap so i dont think network rb is needed
NetworkBehaviour inconsistency on non-Owners
Might be some timing issue where IsOwner might not be set when first spawned on the clients
if IsOwner is not set wouldnt i get an exception trying to set the variables on the OnNetworkSpawn?
readyToInitialize.Value seems that this variable its not synching some times
No, isOwner deaults to false if it's not set. If this is in a player object I normally use IsLocalPlayer. But the network variable should sync eventually. Network Variables are sent reliably.
Have you tried subscribing to readyToInitialize.OnValueChanged event?
Yes, the event its not triggered on the non owner whenever the rest fails
This is not a player object
I didnt put this on the example but i delete the script after initialization, it goes like:
- owner sets
readyToInitialize - next frame in the update
Initializeis called and by the end of that method itDestroy(this)
This variables were sent immediately to the network or could take some frames to send?
Oh you don't ever want to destroy a network behavior.
Network variables are synced on the next network tick