Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Bug NetworkVariable is written to, but doesn't know its NetworkBehaviour yet.

Discussion in 'Netcode for GameObjects' started by Faysou, Jun 11, 2023.

  1. Faysou

    Faysou

    Joined:
    Feb 22, 2021
    Posts:
    12
    Hello !
    I am doing a Timer in my game, here is the script :

    Code (CSharp):
    1. public class MatchManager : NetworkBehaviour
    2. {
    3.     public static readonly NetworkVariable<int> BlueScore = new NetworkVariable<int>();
    4.     public static readonly NetworkVariable<int> OrangeScore = new NetworkVariable<int>();
    5.  
    6.     public static readonly NetworkVariable<float> TimeRemaining = new NetworkVariable<float>(120f);
    7.  
    8.     private GameObject m_Ball;
    9.     private Transform m_BallInitialTransform;
    10.    
    11.     public enum Teams
    12.     {
    13.         Blue,
    14.         Orange
    15.     }
    16.  
    17.     public override void OnNetworkSpawn()
    18.     {
    19.         base.OnNetworkSpawn();
    20.         m_Ball = GameObject.FindWithTag("Ball");
    21.         m_BallInitialTransform = m_Ball.transform;
    22.     }
    23.  
    24.     private void Update()
    25.     {
    26.         if (!IsServer)
    27.             return;
    28.  
    29.         TimeRemaining.Value -= Time.deltaTime;
    30.     }
    31. }
    But when playing my console is flooded with warnings like this one :
    NetworkVariable is written to, but doesn't know its NetworkBehaviour yet.

    Do you have any clues why it is doing that ?
     
  2. Faysou

    Faysou

    Joined:
    Feb 22, 2021
    Posts:
    12
    I am using Unity 2022.3.1f1 LTS and Netcode For GameObjects 1.4.0
     
  3. Faysou

    Faysou

    Joined:
    Feb 22, 2021
    Posts:
    12
    For everyone having the same problem, you should use a singleton instead of making the variables "static".
    I guess having static network variables doesn't make much sense ^^
     
  4. TruckerJoe

    TruckerJoe

    Joined:
    Feb 25, 2019
    Posts:
    39
    If only the server should change the value, you can make this:
    Code (CSharp):
    1. private NetworkVariable<float> TimeRemaining = new NetworkVariable<float>(120f, NetworkVariableReadPermission.Everyone, NetworkVariableWritePermission.Server);
    2.  
     
  5. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,467
    Update() may be running before it is spawned.

    In any case, this is bad practice:
    Code (CSharp):
    1. TimeRemaining.Value -= Time.deltaTime;
    You are making your network variable depend on a single machine's delta time! That spells trouble! This may result in fluctuating time depending on who is updating the timer and whatever hiccups that machine may currently be experiencing.

    You should be using the already synchronized time which is exposed in the NetworkManager as LocalTime and ServerTime. It is measured in ticks, as in "concrete steps" since the concept of "delta time" should not be applied when it comes to updating networked game state.
     
  6. Faysou

    Faysou

    Joined:
    Feb 22, 2021
    Posts:
    12
    Thank you for the feedback I will update this messy code haha