Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Unity Multiplayer Offline scene doesn't load second time server disconnects

Discussion in 'Multiplayer' started by _Adriaan, Jul 22, 2015.

  1. _Adriaan

    _Adriaan

    Joined:
    Nov 12, 2009
    Posts:
    480
    Hello!

    Using the (Unity 5.1.2f1) NetworkManager, when a host or server (on my Mac) disconnects from the internet, all clients (iPhones) automatically disconnect and load the 'Offline' scene. The following will be logged in XCode:

    Log: connection {1} has been disconnected by timeout; address {52.28.24.148:9999}
    Client disconnected
    UNet Client Disconnect Error: Timeout
    NetworkManager:OnClientErrorInternal
    NetworkManager:OnClientDisconnectInternal
    ClientChangeScene newSceneName:Offline networkSceneName:
    NetworkManager StopClient
    Shutting down client 0
    ClientChangeScene newSceneName:Offline networkSceneName:Offline
    Unloading 1 Unused Serialized files (Serialized files now loaded: 0)
    UnloadTime: 5.406458 ms


    However, the second time the server disconnects (without any application restarts), the client doesn't actually change to the Offline scene, logging the following:

    Log: connection {1} has been disconnected by timeout; address {52.28.24.148:9999}
    Client disconnected
    UNet Client Disconnect Error: Timeout
    NetworkManager:OnClientErrorInternal
    NetworkManager:OnClientDisconnectInternal
    ClientChangeScene newSceneName:Offline networkSceneName:Offline
    NetworkManager StopClient
    Shutting down client 1
    ClientChangeScene newSceneName:Offline networkSceneName:Offline


    There is a difference, but not necessarily something that triggers an 'aahh' moment for me. Who can enlighten me?! ;)
     
  2. TheLastVertex

    TheLastVertex

    Joined:
    Sep 30, 2015
    Posts:
    126
    So I ran into this as well, and have finally found a solution, see code snippet below.

    What's interesting is I didn't used to have this issue. I was unable to narrow down the cause but I'm wondering if it's related to changing my network manager to use "don't destroy on load". This would keep it around for multiple matches on the client and may be the root cause. Where previously every time the scene changed the client would have gotten a fresh network manager for each match. This has already taken up a couple days of my time and would take too much work to change everything in the project just to test this, so unfortunately I have to leave this question hanging for the time being.

    I am using NetworkServer.DisconnectAll() to remove all players from the server at the end of the match. I originally thought this maybe the culprit and tried a few alternatives without any luck. Looping through each client connection and using NetworkConnection.Disconnect() instead. Switching to using StopHost() as well. Made no difference.

    So what did work? I overrided OnClientDisconnect() and added the scene change there manually. I'm assuming this is happening anyways, otherwise the first match would have had this issue as well but for w/e reason this has solved it.
    Code (CSharp):
    1.         //Called on clients when disconnected from server
    2.         public override void OnClientDisconnect(NetworkConnection conn)
    3.         {
    4.             //Base functionality - unsure if there is anything in here
    5.             base.OnClientDisconnect(conn);
    6.  
    7.             //Added to force load of offline scene
    8.             SceneManager.LoadScene(offlineScene);
    9.  
    10.         }