Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

Question Beg a way to fix "Cannot start client while an instance is already running"

Discussion in 'Multiplayer' started by unity_qyNAJ9CdbDf9rw, Feb 23, 2024.

  1. unity_qyNAJ9CdbDf9rw

    unity_qyNAJ9CdbDf9rw

    Joined:
    Jun 18, 2019
    Posts:
    10
    "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 !


    Code (CSharp):
    1. public bool StartMyClient()
    2.     {
    3.         if (NetworkManager.Singleton.IsConnectedClient)
    4.         {
    5.             NetworkManager.Singleton.Shutdown();
    6.         }
    7.  
    8.         var success = NetworkManager.Singleton.StartClient();
    9.         if (success)
    10.         {
    11.             //NetworkManager.Singleton.SceneManager.OnSceneEvent += SceneManager_OnSceneEvent;
    12.             IsClient = true;
    13.             BindSceneEvent();
    14.         }
    15.  
    16.         return success;
    17.     }
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    7,263
    This is a troublesome pattern:
    Code (CSharp):
    1. public bool StartMyClient()
    2.     {
    3.         if (NetworkManager.Singleton.IsConnectedClient)
    4.         {
    5.             NetworkManager.Singleton.Shutdown();
    6.         }
    I've seen this a couple times before. It is telling that when you call this method, the application is not fully aware of its state and hasn't properly done a shutdown of the previous session.

    Normally, you'd expect a running session to have ended well before a new one starts - let's say the user is ingame playing online. Then the user quits the game session, this automatically shuts down netcode and returns to the main menu. From the main menu the user choses to participate in another network session.
    I have yet to see a game that lets you start a new online session from within a running session without returning to the menu in between.

    The issue you get - just a hunch - could be related to the fact that you're shutting down and instantly try to start it anew. It may take netcode some background processing until it is fully shutdown.

    Try checking the NetworkManager state right after shutdown. IsListening still true?
    Perhaps the shutdown method is awaitable? If so, await it.
    If not, try adding a coroutine with yield return null once or twice in between to test my hypothesis.
     
    unity_qyNAJ9CdbDf9rw likes this.
  3. unity_qyNAJ9CdbDf9rw

    unity_qyNAJ9CdbDf9rw

    Joined:
    Jun 18, 2019
    Posts:
    10
    Thank you a lot! Help me reconsider my network flow and I think i did it wrong.
    You are right that try to start a new client right after shuting down will cause some hidden error.

    Just one more quest please. When I want to move my party (lobby) to game scene. Can I just disconnect them all then move them to the "big game scene" ?
     
    CodeSmile likes this.
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    7,263
    I haven't used Lobby yet (I hope to get to that soon).

    I tend to assume that once players are in a Lobby, the Host starts the game and all clients get a callback with info on how to join that game session. Afterwards I believe the Lobby is either destructed or kept alive, depending on whether players may want to return to the same Lobby.

    Unity's GitHub has a Lobby sample project: https://github.com/Unity-Technologies/com.unity.services.samples.game-lobby
     
    unity_qyNAJ9CdbDf9rw likes this.