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

Resolved pawnStateException: Object is not spawned error on Disconnecting Clients

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

  1. marzdh

    marzdh

    Joined:
    Jun 3, 2013
    Posts:
    35
    I want a spawned client to move to a new scene. So to begin this move, a ServerRpc is called, where the player is first despawned and then disconnected from the current scene.

    Code (CSharp):
    1. [ServerRpc (RequireOwnership =false)]
    2.     public void DestroyPlayerServerRpc(ServerRpcParams serverRpcParams = default)
    3.     {
    4.         var clientId = serverRpcParams.Receive.SenderClientId;
    5.         if (NetworkManager.ConnectedClients.ContainsKey(clientId))
    6.         {
    7.             if(PlayersInGame.TryGetValue(clientId, out var player))
    8.             {
    9.                 player.GetComponent<NetworkObject>().Despawn(true);
    10.                 Debug.Log(clientId);
    11.                 NetworkManager.Singleton.DisconnectClient(clientId);
    12.             }
    13.             if(clientId == OwnerClientId)
    14.             {
    15.                 NetworkManager.Singleton.Shutdown();
    16.                 SceneManager.LoadSceneAsync("Room 2");
    17.             }
    18.         }
    19.     }
    However when I disconnect, I get an error on the host which says -
    SpawnStateException: Object is not spawned
    Unity.Netcode.NetworkSpawnManager.DespawnObject (Unity.Netcode.NetworkObject networkObject, System.Boolean destroyObject) (at Library/PackageCache/com.unity.netcode.gameobjects@1.4.0/Runtime/Spawning/NetworkSpawnManager.cs:670)

    The client however does move to the next scene.

    How can I disconnect clients and move them to new scenes without affecting the first scene where all clients assemble.
     
  2. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    674
    You don't need an rpc for this. Subscribe to the OnClientStopped event and inside that callback method have your LoadScene call. Calling Shutdown on the client will trigger the OnClientStopped event.

    For example:
    Code (CSharp):
    1.     public void OnClickStartClient()
    2.     {
    3.         NetworkManager.Singleton.OnClientStopped += OnClientStopped;
    4.         NetworkManager.Singleton.StartClient();
    5.     }
    6.  
    7.     public void OnClickShutdown()
    8.     {
    9.         NetworkManager.Singleton.Shutdown();
    10.     }
    11.  
    12.     private void OnClientStopped(bool wasHost)
    13.     {
    14.         Debug.Log("OnClientStopped wasHost: " + wasHost);
    15.  
    16.         SceneManager.LoadSceneAsync("Room 2");
    17.     }