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

Question Destroy a spawned NetworkObject on a non-host client is not valid.

Discussion in 'Netcode for GameObjects' started by TruckerJoe, Aug 13, 2023.

  1. TruckerJoe

    TruckerJoe

    Joined:
    Feb 25, 2019
    Posts:
    39
    Hallo,

    I get a this error, if I start the game with 4 or more players:

    [Netcode-Server Sender=3] Destroy a spawned NetworkObject on a non-host client is not valid. Call Destroy or Despawn on the server/host instead.
    UnityEngine.Debug:LogError (object)
    Unity.Netcode.NetworkLog:LogErrorServerLocal (string,ulong) (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.5.1/Runtime/Logging/NetworkLog.cs:111)
    Unity.Netcode.ServerLogMessage:Handle (Unity.Netcode.NetworkContext&) (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.5.1/Runtime/Messaging/Messages/ServerLogMessage.cs:51)
    Unity.Netcode.NetworkMessageManager:ReceiveMessage<Unity.Netcode.ServerLogMessage> (Unity.Netcode.FastBufferReader,Unity.Netcode.NetworkContext&,Unity.Netcode.NetworkMessageManager) (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.5.1/Runtime/Messaging/NetworkMessageManager.cs:564)
    Unity.Netcode.NetworkMessageManager:HandleMessage (Unity.Netcode.NetworkMessageHeader&,Unity.Netcode.FastBufferReader,ulong,single,int) (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.5.1/Runtime/Messaging/NetworkMessageManager.cs:432)
    Unity.Netcode.NetworkMessageManager:ProcessIncomingMessageQueue () (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.5.1/Runtime/Messaging/NetworkMessageManager.cs:458)
    Unity.Netcode.NetworkManager:NetworkUpdate (Unity.Netcode.NetworkUpdateStage) (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.5.1/Runtime/Core/NetworkManager.cs:42)
    Unity.Netcode.NetworkUpdateLoop:RunNetworkUpdateStage (Unity.Netcode.NetworkUpdateStage) (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.5.1/Runtime/Core/NetworkUpdateLoop.cs:185)
    Unity.Netcode.NetworkUpdateLoop/NetworkEarlyUpdate/<>c:<CreateLoopSystem>b__0_0 () (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.5.1/Runtime/Core/NetworkUpdateLoop.cs:208)


    If I start with 1-3 players, everything works fine.
    If I start with 3 players, and 3 more players are late joining the game, everthing works fine.
    Only if more then 3 players are in the lobby at game start, I get this error.

    I can´t see in console where the error is happening in my code.
    Is there a trick how I can see it?
    Any ideas what the reason for this is?

    I am using:
    Unity 2022.3.0
    Netcode: 1.5.1
    Lobby: 1.0.3
    Relay: 1.0.5
     
  2. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    677
    This is a guess but it sounds like the client is switching to another scene when a network object has already been spawned on the first scene, that shouldn't happen when using the default scene management.

    You might be able to work out what network object is missing on the client, that's if the exception hasn't stopped the other network objects spawning.
     
  3. TruckerJoe

    TruckerJoe

    Joined:
    Feb 25, 2019
    Posts:
    39
    Thank you for your answer @cerestorm
    Yes, that's what it looks like.
    I have checked the "Enable Scene Management" in the Network Manager.
    And the host is changing the scene with:
    Code (CSharp):
    1. NetworkManager.Singleton.SceneManager.LoadScene(targetScene.ToString(), LoadSceneMode.Single);
    The is the default scene management, isn´t it?
     
  4. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    677
    It is, it seems like a strange issue so it could be a bug, but I've not used Lobby or Relay so don't know if they factor into it.

    It may help to know which network object it is, on the host you could check all the NetworkObjects in the inspector under Observers to see if a client is missing but with this issue it may still report it as observed by that client. Failing that if the client's network manager is still running check for a missing network object in the scene. Also in theory if the host still has that client as observing the network object you should see warnings if any messages/changes are being sent to that object on the client.
     
  5. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    677
    I did quick test to reproduce the error, you should be able to see which network object it is by setting the Log Level of the network manager to Developer in the inspector.
     
  6. TruckerJoe

    TruckerJoe

    Joined:
    Feb 25, 2019
    Posts:
    39
    Yes, I found the network objects.
    I am spawning some ammo and health packs in the game scene.
    For this I use a SpawnManager(NetworkObject), and in the OnNetworkSpawn() I spawn the ammo and heath packs.
    If I comment these lines out, and the error is no longer there, BUT of course there are no ammo and health packs in the scene for the players:(
    Where should I spawn them?
     
  7. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    677
    In whose OnNetworkSpawn() are you spawning the ammo and health packs, the Player's? Shouldn't you also be getting the same error for that object as it would be spawned first, or am I not understanding you. Either way it would useful to log all OnNetworkSpawns of suspect objects as spawning should happen in order (or at least it did a long time ago when I last checked) and check if any are logged before the scene change.

    As for delaying spawning the ammo/health, you can send a ServerRpc message when the Player spawns, or when the server is notified the client's scene is loaded, it depends on what fits best within the project.
     
    TruckerJoe likes this.
  8. TruckerJoe

    TruckerJoe

    Joined:
    Feb 25, 2019
    Posts:
    39
    In the gamescene I have an Spawnmanager (Empty Object with Network Object Component attached) and in the OnNetworkSpawn() of this Spawnmanager I spawn the ammo/health:
    Code (CSharp):
    1. public override void OnNetworkSpawn()
    2.     {
    3.         Instance = this;
    4.  
    5.         if (IsServer)
    6.         {
    7.  
    8.             // Shuffle the spawnPoints
    9.             ShuffleSpawnPoints();
    10.  
    11.             // Create Flag
    12.             SpawnFlag();
    13.            
    14.             // Create PickupObjects
    15.             while (currentPickUpObjectsCount < spawnPickUpObjectCountAtStart)
    16.             {
    17.                 SpawnPickupObjects();
    18.                 currentPickUpObjectsCount++;
    19.             }
    20.         }
    21.     }
    If all players are connected, I start a countdown.
    If I spawn the ammo/health at this point, after a few tests, all is fine.
    I think I will do it now here, but I still not understand why my first attempt wasn´t successful.
    The Server create the scene, and he have to syncronize the clients when they connect.

    Thank you for great help!