Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question NetworkBehaviour is being destroyed while NetworkObject is still spawned!

Discussion in 'Netcode for GameObjects' started by hawaiian_lasagne, Jun 18, 2023.

  1. hawaiian_lasagne

    hawaiian_lasagne

    Joined:
    May 15, 2013
    Posts:
    120
    I keep getting this warning message generated when a client disconnects from the host:

    [Netcode] NetworkBehaviour-Default is being destroyed while NetworkObject-Default is still spawned! (could break state synchronization)
    UnityEngine.Debug:LogWarning (object)

    The Default object in this case is the player's prefab (very simple, 1 component)

    Any idea what might be causing this?

    TBH though, I thought network behaviours would get destroyed before the NetworkObject anyway, so the warning is a little bit cryptic as to what exactly is going wrong.
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,899
    No code, no insight. There is only guessing.

    My best guess: don't call destroy in OnNetworkSpawn. Maybe that?
     
  3. hawaiian_lasagne

    hawaiian_lasagne

    Joined:
    May 15, 2013
    Posts:
    120
    Here are the two components for my player prefab:

    Code (CSharp):
    1. public class NetworkName : NetworkBehaviour {
    2.     public NetworkVariable<FixedString32Bytes> PlayerName = new NetworkVariable<FixedString32Bytes>("Default", NetworkVariableReadPermission.Everyone, NetworkVariableWritePermission.Owner);
    3. }
    Code (CSharp):
    1.  
    2. public class ConnectedPlayer : NetworkBehaviour {
    3.  
    4.     NetworkName networkName;
    5.  
    6.     public ulong PlayerId {
    7.         get {
    8.             if (networkName != null) {
    9.                 return OwnerClientId;
    10.             }
    11.             return 666;
    12.         }
    13.     }
    14.  
    15.     public string PlayerName {
    16.         get {
    17.             if (networkName != null) {
    18.                 return networkName.PlayerName.Value.ToString();
    19.             }
    20.             return "UNKNOWN";
    21.         }
    22.  
    23.         set {
    24.             if (networkName != null && IsOwner) {
    25.                 networkName.PlayerName.Value = value;
    26.             } else {
    27.                 Debug.Log("ERROR TRYING TO SET PLAYER NAME: " + value);
    28.             }
    29.         }
    30.     }
    31.  
    32.     public override void OnNetworkSpawn() {
    33.         base.OnNetworkSpawn();
    34.         if (!TryGetComponent(out networkName)) {
    35.             Debug.Log("Failed to get network name");
    36.         } else {
    37.             if (IsOwner) {
    38.                 networkName.PlayerName.Value = "Player" + OwnerClientId;
    39.             }
    40.             gameObject.name = networkName.PlayerName.Value.ToString();
    41.         }
    42.  
    43.         App.ConnectedPlayers.OnPlayerNetworkSpawn(this);
    44.     }
    45.  
    46.     public override void OnNetworkDespawn() {
    47.         App.ConnectedPlayers.OnPlayerNetworkDespawn(this);
    48.         base.OnNetworkDespawn();
    49.     }
    50.  
    51.     public override string ToString() {
    52.         return $"[{PlayerName}:{PlayerId}]";
    53.     }
    54. }
    55.  
    I've tried disabling the OnPlayerNetworkDespawn event invoked from OnNetworkDespawn but still seeing the error
     
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,899
    What is that doing?

    If this sends out events to one or more subscribers you should check what each of them is doing when this event gets fired.
     
  5. hawaiian_lasagne

    hawaiian_lasagne

    Joined:
    May 15, 2013
    Posts:
    120
    Oh, one thing to note, it only happens after I've switched scenes and the client disconnects from the host.
     
  6. hawaiian_lasagne

    hawaiian_lasagne

    Joined:
    May 15, 2013
    Posts:
    120
    As I said, I tried disabling that line and the warning still occurs. Nothing acts on the Disconnect right now, which is where I'm seeing the issue
     
  7. hawaiian_lasagne

    hawaiian_lasagne

    Joined:
    May 15, 2013
    Posts:
    120
    I think I've figured it out.

    I was calling networkObject.SpawnAsPlayerObject instead of networkObject.SpawnWithOwnership for creating the player when the game scene is loaded.

    There was already a player prefab assigned to the network manager (code above) so I think SpawnWithOwnership is the right one to use for the in game player object.