Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How to fix the sync of my networkVariable

Discussion in 'Scripting' started by Mikonoru, May 29, 2023.

  1. Mikonoru

    Mikonoru

    Joined:
    Jan 21, 2023
    Posts:
    2
    Hello, I'm currently working on allowing a client to modify a 'NetworkVariable<bool>' and have it be changed for every other client as well. However, it seems to only work in theory.

    As a test, I added a ClientRpc method that outputs a warning. The warning message is as follows:

    [Netcode] Deferred messages were received for a trigger of type OnSpawn with key 0, but that trigger was not received within within 1 second(s).
    UnityEngine.Debug:LogWarning (object)
    Unity.Netcode.NetworkLog:LogWarning (string) (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.2.0/Runtime/Logging/NetworkLog.cs:28)
    Unity.Netcode.DeferredMessageManager:PurgeTrigger (Unity.Netcode.IDeferredMessageManager/TriggerType,ulong,Unity.Netcode.DeferredMessageManager/TriggerInfo) (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.2.0/Runtime/Messaging/DeferredMessageManager.cs:98)
    Unity.Netcode.DeferredMessageManager:CleanupStaleTriggers () (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.2.0/Runtime/Messaging/DeferredMessageManager.cs:83)
    Unity.Netcode.NetworkManager:OnNetworkPostLateUpdate () (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.2.0/Runtime/Core/NetworkManager.cs:1648)
    Unity.Netcode.NetworkManager:NetworkUpdate (Unity.Netcode.NetworkUpdateStage) (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.2.0/Runtime/Core/NetworkManager.cs:1538)
    Unity.Netcode.NetworkUpdateLoop:RunNetworkUpdateStage (Unity.Netcode.NetworkUpdateStage) (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.2.0/Runtime/Core/NetworkUpdateLoop.cs:185)
    Unity.Netcode.NetworkUpdateLoop/NetworkPostLateUpdate/<>c:<CreateLoopSystem>b__0_0 () (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.2.0/Runtime/Core/NetworkUpdateLoop.cs:268)




    Unfortunately, the value is only being changed on the client itself, and other clients are not being notified of the change.

    my Code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using Unity.Netcode;
    4. using UnityEngine;
    5.  
    6. public class InteractableObject : NetworkBehaviour
    7. {
    8.     public static InteractableObject Instance;
    9.  
    10.     public NetworkVariable<bool> isPoisen = new NetworkVariable<bool>(false);
    11.     public NetworkVariable<float>poisenTime = new NetworkVariable<float>(100f);
    12.  
    13.     private void Awake()
    14.     {
    15.         Instance = this;
    16.     }
    17.  
    18.     private void Start()
    19.     {
    20.         poisenTime.Value = 100f;
    21.     }
    22.  
    23.     void Update()
    24.     {
    25.         if (isPoisen.Value)
    26.         {
    27.             poisenTime.Value -= Time.deltaTime;
    28.             if (poisenTime.Value <= 0)
    29.             {
    30.                 isPoisen.Value = false;
    31.                 poisenTime.Value = 100f;
    32.             }
    33.         }
    34.        
    35.     }
    36.  
    37.     // Get the poison state
    38.     public bool GetPoisenState()
    39.     {
    40.         return isPoisen.Value;
    41.     }
    42.  
    43.     // Set the poison state (called by the player who interacts with the object)
    44.     public void SetPoisenState(bool poisenState)
    45.     {
    46.         SetPoisenStateServerRpc(poisenState);
    47.     }
    48.  
    49.     [ServerRpc(RequireOwnership = false)]
    50.     private void SetPoisenStateServerRpc(bool poisenState)
    51.     {
    52.         this.isPoisen.Value = poisenState;
    53.         ClientMessageClientRpc(poisenState);
    54.     }
    55.  
    56.     [ClientRpc]
    57.     private void ClientMessageClientRpc(bool poisenState)
    58.     {
    59.         Debug.Log("test");
    60.     }
    61. }

    The object with which I want to interact has a NetworkObject component, and its prefab has been added to the NetworkManager. Only the prefab has been placed.

    If you need any further information, please feel free to ask.
     
  2. WaiveRyder

    WaiveRyder

    Joined:
    Jul 26, 2021
    Posts:
    1
    I have a similar issue where I get the warning:
    [Netcode] Deferred messages were received for a trigger of type OnSpawn with key 0, but that trigger was not received within within 1 second(s)
    It seems that it has something to do with calling a ServerRpc as I didn't have this warning beforehand, and this warning only shows up when playing with 2 instances (a host and a client connected). In my ServerRpc I change a NetworkVariable and everything seems to work yet I get that warning. Wondering what I am doing wrong.