Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Bug Cannot start Host while an instance is already running

Discussion in 'Netcode for GameObjects' started by Iron-Phuong, Apr 30, 2024.

  1. Iron-Phuong

    Iron-Phuong

    Joined:
    Jun 22, 2015
    Posts:
    9
    Hi everybody,

    My game occasionally loses connection, the lobby works normally but when 2 players are ready, they cannot connect to each other. Here is the error:

    2024/04/30 14:16:28.452 19535 19595 Warn Unity [Netcode] Cannot start Host while an instance is already running
    2024/04/30 14:16:28.452 19535 19595 Warn Unity Unity.Netcode.NetworkManager:CanStart(StartType)
    2024/04/30 14:16:28.452 19535 19595 Warn Unity Unity.Netcode.NetworkManager:StartHost()
    2024/04/30 14:16:28.452 19535 19595 Warn Unity <CreateNetworkManager>d__55:MoveNext()
    2024/04/30 14:16:28.452 19535 19595 Warn Unity System.Threading.ExecutionContext:RunInternal(ExecutionContext, ContextCallback, Object, Boolean)
    2024/04/30 14:16:28.452 19535 19595 Warn Unity System.Runtime.CompilerServices.MoveNextRunner:Run()
    2024/04/30 14:16:28.452 19535 19595 Warn Unity System.Threading.Tasks.AwaitTaskContinuation:RunCallback(ContextCallback, Object, Task&)
    2024/04/30 14:16:28.452 19535 19595 Warn Unity System.Threading.Tasks.Task:FinishContinuations()
    2024/04/30 14:16:28.452 19535 19595 Warn Unity System.Threading.Tasks.Task`1:TrySetResult(TResult)
    2024/04/30 14:16:28.452 19535 19595 Warn Unity System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1:SetResult(TResult)
    2024/04/30 14:16:28.452 19535 19595 Warn Unity <SetRelayHostData>d__57:MoveNext()
    2024/04/30 14:16:28.452 19535 19595 Warn Unity System.Threading.ExecutionContext:RunInternal(ExecutionContext, ContextCallback, Object, Boolean)
    2024/04/30 14:16:28.452 19535 19595 Warn Unity System.Runtime.CompilerServices.MoveNextRunner:Run()
    2024/04/30 14:16:28.452 19535 19595 Warn Unity System.Threading.Tasks.AwaitTaskContinuation:RunCallback(ContextCallback, Object, Task&)
    2024/04/30 14:16:28.452 19535 19595 Warn Unity System.Threading.Tasks.Task:FinishContinuations()
    2024/04/30 14:16:28.452 19535 19595 Warn Unity System.Threading.Tasks.Task`1:TrySetResult(TResult)
    2024/04/30 14:16:28.452 19535 19595 Warn Unity System.Runtime.CompilerServices.As
    2024/04/30 14:16:28.653 19535 19595 Warn Unity [Netcode] Cannot start Host while an instance is already running
    2024/04/30 14:16:28.653 19535 19595 Warn Unity Unity.Netcode.NetworkManager:CanStart(StartType)
    2024/04/30 14:16:28.653 19535 19595 Warn Unity Unity.Netcode.NetworkManager:StartHost()
    2024/04/30 14:16:28.653 19535 19595 Warn Unity <CreateNetworkManager>d__55:MoveNext()
    2024/04/30 14:16:28.653 19535 19595 Warn Unity System.Threading.ExecutionContext:RunInternal(ExecutionContext, ContextCallback, Object, Boolean)
    2024/04/30 14:16:28.653 19535 19595 Warn Unity System.Runtime.CompilerServices.MoveNextRunner:Run()
    2024/04/30 14:16:28.653 19535 19595 Warn Unity System.Threading.Tasks.AwaitTaskContinuation:RunCallback(ContextCallback, Object, Task&)
    2024/04/30 14:16:28.653 19535 19595 Warn Unity System.Threading.Tasks.Task:FinishContinuations()
    2024/04/30 14:16:28.653 19535 19595 Warn Unity System.Threading.Tasks.Task`1:TrySetResult(TResult)
    2024/04/30 14:16:28.653 19535 19595 Warn Unity System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1:SetResult(TResult)
    2024/04/30 14:16:28.653 19535 19595 Warn Unity <SetRelayHostData>d__57:MoveNext()
    2024/04/30 14:16:28.653 19535 19595 Warn Unity System.Threading.ExecutionContext:RunInternal(ExecutionContext, ContextCallback, Object, Boolean)
    2024/04/30 14:16:28.653 19535 19595 Warn Unity System.Runtime.CompilerServices.MoveNextRunner:Run()
    2024/04/30 14:16:28.653 19535 19595 Warn Unity System.Threading.Tasks.AwaitTaskContinuation:RunCallback(ContextCallback, Object, Task&)
    2024/04/30 14:16:28.653 19535 19595 Warn Unity System.Threading.Tasks.Task:FinishContinuations()
    2024/04/30 14:16:28.653 19535 19595 Warn Unity System.Threading.Tasks.Task`1:TrySetResult(TResult)
    2024/04/30 14:16:28.653 19535 19595 Warn Unity System.Runtime.CompilerServices.As
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,992
    Sounds like the host is already running a game as either host or client. Check your netcode shutdown logic. Note that you may have to let some time pass between disconnect and Start… calls.

    For further analysis post your code.
     
    Iron-Phuong likes this.
  3. Iron-Phuong

    Iron-Phuong

    Joined:
    Jun 22, 2015
    Posts:
    9
    Thank you for your quick reply, I have used NetworkManager.Singleton.Shutdown(true); to disconnect when the user exits the game, in addition to lobby handling logic. I can't determine where this error comes from?
     
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,992
    Does the error occur only when the user had hosted a game before?

    If so the Shutdown isn't complete, or not happening. Check if perhaps you have duplicate NetworkManager instances in your scene. This is a common issue when you have NetworkManager in a scene that you're loading again. Therefore NetworkManager ought to be in some sort of "launch" scene that is never loaded again.
     
    Iron-Phuong likes this.
  5. Iron-Phuong

    Iron-Phuong

    Joined:
    Jun 22, 2015
    Posts:
    9
    Thank you, I have fixed this error. This is because NetworkManager is not destroyed and duplicated many times
     
    CodeSmile likes this.
  6. Iron-Phuong

    Iron-Phuong

    Joined:
    Jun 22, 2015
    Posts:
    9
    For those who encounter this error, if you start the host many times in a short period of time it will also cause this error.