#NetworkBehaviour inconsistency on non-Owners

1 messages · Page 1 of 1 (latest)

shrewd moon
#
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

fossil idol
#

Might be some timing issue where IsOwner might not be set when first spawned on the clients

shrewd moon
#

if IsOwner is not set wouldnt i get an exception trying to set the variables on the OnNetworkSpawn?

shrewd moon
#

readyToInitialize.Value seems that this variable its not synching some times

fossil idol
shrewd moon
#

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 Initialize is called and by the end of that method it Destroy(this)

This variables were sent immediately to the network or could take some frames to send?

fossil idol
shrewd moon
#

Ever? 😅 Ups

#

Is there a way to "wait" for next tick? To safely delete?

#

After all this i think it will be much easier to have a monobehaviour "spawn" then a networkbehaviour actual asset. Been stuck for 2 days trying to make this the "easy way" lol