Player objects persisting after disconnecting

After wiring up the Lobby Menu to display player names, I’m now noticing that my Player Objects are not being destroyed when a client disconnects from the server in the lobby. The server keeps displaying both Player 1 and Player 2 after Player 2 has disconnected, and if the client reconnects, the client will display three names, Player 1, Player 2, and Player 2 repeated. Server does not display an extra Player 2 but does show the extra Player object in Unity. Leaving the lobby and joining again will spawn and display yet another Player 2 on the client.

This deep into the course, I’m not sure what I’ve done wrong or when it happened, and I’ve not yet developed the intuition to make an educated guess. What missing lines of code would be responsible for destroying Player objects when a client disconnects? And around which lecture might I have missed those lines?

Hi there, destroying the player object should be handled by the NetworkManager when the player disconnects, so my instinct is that the player is not disconnecting correctly, or the host is not receiving the message that your client has disconnected. I assume your LevaeLobby() code is correct, but best to check that first.

public void LeaveLobby()
    {
        if(NetworkServer.active && NetworkClient.isConnected)
        {
            NetworkManager.singleton.StopHost();
        }
        else
        {
            NetworkManager.singleton.StopClient();
            SceneManager.LoadScene(0);
        }
    }

I think the second thing is to make sure your RTSNetworkManager has the correct base code running on the ooverride methods:
base.OnClientDisconnect(conn);

public override void OnClientDisconnect(NetworkConnection conn)
    {
        base.OnClientDisconnect(conn);
        ClientOnDisconnected?.Invoke();
    }

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms