Search Unity

Question NetworkVariable not updating properly

Discussion in 'Multiplayer' started by herra_lehtiniemi, May 6, 2022.

  1. herra_lehtiniemi

    herra_lehtiniemi

    Joined:
    Feb 12, 2017
    Posts:
    133
    Using Unity 2022.2.0a9 with Netcode for GameObjects 1.0.0-pre8.

    I'm running the project on one Host and one Client. When the client tries to update a NetworkVariable (by using ServerRpc), the call is executed on Host/Server and proper value is seen by the Host on _UpdatePositionServerRpc-function call, but the variable has reverted back to default value when checked in the Update-loop of the same script. Here's the code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Unity.Netcode;
    5. using UnityEngine.InputSystem;
    6.  
    7. public class FPCController : NetworkBehaviour
    8. {
    9.     [SerializeField]
    10.     private NetworkVariable<Vector3> _serverMovement = new NetworkVariable<Vector3>();
    11.    
    12.     private Vector3 _localDeltaPosition = new Vector3(0, 0);
    13.     private Vector3 _localDeltaPositionPrevious = new Vector3(0, 0);
    14.    
    15.     public float walkSpeed = 0.05f;
    16.    
    17.     // Start is called before the first frame update
    18.     void Start()
    19.     {
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update()
    24.     {
    25.         if (IsServer)
    26.         {
    27.             Debug.Log($"ServerMovement.Value is now on server {_serverMovement.Value}");
    28.             _UpdateServer();
    29.         }
    30.  
    31.         if (IsClient) {
    32.             Debug.Log($"ServerMovement.Value is now on client {_serverMovement.Value}");
    33.             // Read local keyboard strokes and ask for server update if movement occurs
    34.             _UpdateClient();
    35.         }
    36.     }
    37.  
    38.     private void _UpdateServer()
    39.     {
    40.         transform.position += _serverMovement.Value;
    41.     }
    42.  
    43.     public void _UpdateClient() {
    44.         _localDeltaPosition = new Vector3();
    45.        
    46.        
    47.         if (Keyboard.current.upArrowKey.isPressed) {
    48.             _localDeltaPosition += new Vector3(0, 0, walkSpeed);
    49.         }
    50.  
    51.         if (Keyboard.current.downArrowKey.isPressed)
    52.         {
    53.             _localDeltaPosition += new Vector3(0, 0, -walkSpeed);
    54.         }
    55.  
    56.         if (Keyboard.current.leftArrowKey.isPressed)
    57.         {
    58.             _localDeltaPosition += new Vector3(-walkSpeed, 0, 0);
    59.         }
    60.  
    61.         if (Keyboard.current.rightArrowKey.isPressed)
    62.         {
    63.             _localDeltaPosition += new Vector3(walkSpeed, 0, 0);
    64.         }
    65.        
    66.         if (_localDeltaPosition != _localDeltaPositionPrevious) {
    67.             _localDeltaPositionPrevious = _localDeltaPosition;
    68.  
    69.             // Update variables on server version
    70.             Debug.Log($"Calling UpdatePositionServerRpc with value {_localDeltaPosition}");
    71.  
    72.             _UpdatePositionServerRpc(_localDeltaPosition);
    73.         }
    74.     }
    75.    
    76.     // Update variables on server
    77.     [ServerRpc(RequireOwnership = false)]
    78.     private void _UpdatePositionServerRpc(Vector3 newMovement) {
    79.         Debug.Log($"Updating position on ServerRpc with movement value {newMovement}");
    80.         _serverMovement.Value = newMovement;
    81.         Debug.Log($"ServerMovement.Value is now {_serverMovement.Value}");
    82.     }
    83.    
    84.    
    85. }
    86.  
    • The client called _UpdatePositionServerRpc to deliver the movement to server
    • The call is executed properly on Host-client. The Debug.Log($"ServerMovement.Value is now...") reflects the proper variable value
    • However, on the Update() at if (IsServer), the _serverMovement.Value is constantly 0.
    What on earth could cause this? This means that the value and the Rpc request are both transferred properly to the correct Host client and the value is seen by the Host and changed inside _UpdatePositionServerRpc, but after exiting _UpdatePositionServerRpc, the _serverMovement.Value becomes (0, 0, 0) again when checked in the Update-loop (in the IsServer-section).

    I have checked the following:
    • my script inherits from NetworkBehaviour
    • the script is attached to a prefab that has both NetworkObject and NetworkTransform
    • the prefab is added to NetworkPrefabs-list on NetworkManager.

    I can't figure out what's happening here.