"Beg a way to fix ""Cannot start client while an instance is already running""" (339426)

Hello !

I have a game that when player login to the game, they will host themself and enter “scene A”. Then I use Netcode for Gameobject service “Matchmaking” to matchmake “Player A” with “Player B”.

I tried to Shutdown the Host by player and connect to deliciated server by StartClient(). But I got error “Cannot start client while an instance is already running”.

One thought: I tried to link my problem with when the game have multiple lobby and join one game server, how pro do it ? Like PUBG, each room have 4 player and move to big scene. I mean how “pro” handle shutdown lobby and join big game scene ?

Thanks for advice !

public bool StartMyClient()
    {
        if (NetworkManager.Singleton.IsConnectedClient)
        {
            NetworkManager.Singleton.Shutdown();
        }

        var success = NetworkManager.Singleton.StartClient();
        if (success)
        {
            //NetworkManager.Singleton.SceneManager.OnSceneEvent += SceneManager_OnSceneEvent;
            IsClient = true;
            BindSceneEvent();
        }

        return success;
    }

This will not work:

NetworkManager.Singleton.Shutdown();
NetworkManager.Singleton.StartClient();

Shutdown() is not instantaneous!

At least not for the server/host since the server has to wait for all clients to report back that they have disconnected. How much time this takes varies for obvious reasons.


In order to properly restart a session you have to wait until IsServer or IsClient are false after calling Shutdown(). You can do so within a coroutine for example:

var net = NetworkManager.Singleton;
while (net.IsServer || net.IsClient)
    yield return null;

NetworkManager.Singleton.StartClient();

StartClient() is just an example, you also have to do the same if you intend to StartHost() or StartServer().