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 Despawning a Network Object throws a SpawnStateException

Discussion in 'Netcode for GameObjects' started by marzdh, Sep 8, 2023.

  1. marzdh

    marzdh

    Joined:
    Jun 3, 2013
    Posts:
    31
    In the Network Manager I have not defined a Player Prefab, but registered it in the NetworkPrefab list. The reason for this is, I want to spawn it during runtime.
    So after I execute the code NetworkManager.Singleton.StartHost() or Networkmanager.Singleton.StartClient()
    I call the function to spawn the player from the server.
    Code (CSharp):
    1. private void Singleton_OnClientConnectedCallback(ulong id)
    2.     {
    3.         Debug.Log($"Id Connected {id} and OwnerClient id {OwnerClientId}");
    4.         myId = id;
    5.          
    6.         if(IsOwner)
    7.             CreatePlayerServerRpc(id);
    8.     }
    9.  
    10.     [ServerRpc]
    11.     public void CreatePlayerServerRpc(ulong id)
    12.     {
    13.         player = Instantiate(playerPrefab, Vector3.zero, Quaternion.identity);
    14.         player.GetComponent<NetworkObject>().SpawnAsPlayerObject(id);
    15.         playersInGame.Add(id, player);
    16.     }
    Now when I want to Despawn the player I execute the code below -

    Code (CSharp):
    1. public void DestroyPlayer()
    2.     {
    3.          DestroyPlayerServerRpc();
    4.     }
    5.  
    6.     [ServerRpc (RequireOwnership =false)]
    7.     public void DestroyPlayerServerRpc(ServerRpcParams serverRpcParams = default)
    8.     {
    9.         var clientId = serverRpcParams.Receive.SenderClientId;
    10.         if (NetworkManager.ConnectedClients.ContainsKey(clientId))
    11.         {
    12.             if(PlayersInGame.TryGetValue(clientId, out var player))
    13.             {
    14.              
    15.                 player.GetComponent<NetworkObject>().Despawn(true);
    16.                 NetworkManager.Singleton.SceneManager.LoadScene("MeetingRoom 1", LoadSceneMode.Additive);
    17.             }
    18.         }
    19.     }
    But the Despawn code throws an error -
    SpawnStateException: Object is not spawned
    at Unity.Netcode.NetworkSpawnManager.DespawnObject

    Does anyone know what this error means? I want to despawn the player and then move to the next scene.
     
  2. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    603
    I tested with your code (mostly) and it works. What is the network object you have those methods on, you're checking IsOwner, is there more than one of them?
     
  3. marzdh

    marzdh

    Joined:
    Jun 3, 2013
    Posts:
    31
    The network object is a player manager that is present on all clients. So my understanding is (I could be wrong), when a new client connects, the OnClientConnectedCallback is executed on all clients. However it should only be the newly connected client that should be sending an RPC to the server to spawn their player prefab.

    But what does the error really mean? Is the sequence of the code correct for despawning and disconnecting a client?
     
  4. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    603
    OnClientConnectedCallback is called on all clients but with the host being the owner you don't need it to call an rpc to spawn the players.

    I'm not sure on what's causing the error, in Play mode check the player is in the hierarchy window and that its Network Object component has a NetworkObjectId and observer.