I am Confused and can't understand NetworkVariable !

public NetworkVariable<Vector3> PositionVar = new NetworkVariable<Vector3>(Vector3.zero);
 void Update()
    {
Debug.Log($"pos {PositionVar.Value}");
 transform.position = PositionVar.Value;//set by the rpc caller only

}
 [ServerRpc]
    public void RequestSetPositionByNetVarServerRpc()
    {
        PositionVar.Value +=new Vector3(0,0, 20);
    }

The problem: When Client A fires an RPC to change a variable, the update function on Client B shows the logs of the change, but the position value is never set.
On client A the logs are printed and the position change.
same for Client B , the rpc caller position is changed only .
What I understand: When Client A fires an RPC, it sends packets to the server, which then invokes the function “RequestSetPositionByNetVarServerRpc” on the server side. The server then changes the value of the “PositionVar” and sends packets with new values to all clients. If I understand correctly, the “PositionVar” is bound to the network instance on the server side of player A. So, when the server synchronizes, it syncs an ID or something related to the object in the form of “obj-PositionVar”? However, I am not sure why the PositionVar.Value is not set on other clients (e.g. Client B, C and D) even though they can show the PositionVar. I also haven’t checked for the Owner of the local player, and I am not sure how Unity handles this.

Hi @mooooi5 , NetworkVariables are edited on the server (I.E: in the RPC you wrote) and synchronized back to the GameObject’s instance on all clients.

You don’t need to update the variable every Update, there’s an event you can register to (have a look at the examples on the doc).

In you case, are you sure the ServerRPC is actually been executed? There might be an ownership problem. You can add a log in the RPC to be sure of it.

@PaoloAbela
"I have understood after trying all day and I will explain it. Correct me if I am wrong please. I didn’t understand that each spawned NetworkBehaviour has two instances, one on the server and one for the client. So, there is an update function running on the server and an update for the client for the same network object.

For example, let’s say we have clients A, B, and C. If client A sends an RPC request, the server will update the variable as ClientX.networkvar.value for all the clients.
In this line: transform.position = PositionVar.Value,
it will update the position in three places: 1- in the server instance of the server player. 2- in the local client who sent the RPC. 3- in other clients (Client B will get the packet, the packet will go to the Client A object instance which is in Client B’s (Scene or memory or screen). If Client A sends the RPC, then the server will replicate to all clients something like ClientA.PositionVar."

@mooooi5 Actually, the 2nd and 3rd place are updated at the same time. When the value of PositionVar changes on the server for player A, all instances of player A on all clients will be updated. PositionVar of player B and C will not be touched.

Does this clarify it a bit?

1 Like

@PaoloAbela sure thank you

1 Like