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.
[ServerRpc (RequireOwnership =false)]
public void DestroyPlayerServerRpc(ServerRpcParams serverRpcParams = default)
{
var clientId = serverRpcParams.Receive.SenderClientId;
if (NetworkManager.ConnectedClients.ContainsKey(clientId))
{
if(PlayersInGame.TryGetValue(clientId, out var player))
{
player.GetComponent<NetworkObject>().Despawn(true);
Debug.Log(clientId);
NetworkManager.Singleton.DisconnectClient(clientId);
}
if(clientId == OwnerClientId)
{
NetworkManager.Singleton.Shutdown();
SceneManager.LoadSceneAsync("Room 2");
}
}
}
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.