How to Disconnect Clients properly?

I’ve been getting to grips with uNet, using the Lobby Example as a base. The one thing I can’t work out or find suitable information on is on how to disconnect Clients correctly.

The game itself is a 1 vs 1, so one person hosts and the other player is a Client. I’ve added a “disconnect” button and using this code when the button is pressed:

NetworkManager.singleton.client.Disconnect();

This works as intended for the Host and returns them to the Offline Scene. I can’t get it to work for the Client, it just stops messages to/from the server, but fails to change the scene or run “OnClientDisconnect” on the LobbyManager script which it has been doing for the Host.

Is this a bug or am I doing things wrongly?

I use StopHost/StopClient for this purpose.

1 Like

Hi, it seems you use the NetworkManager, why not use:

NetworkManager.singleton.StopClient();

For the Client and

NetworkManager.singleton.StopHost();

For your Host Player

1 Like

Thanks for the quick replies, I’ll try that out

It works! And works with the OnStopClient() override, thanks!

I know this is an old thread but it’s a top Google search result. What finally solved it for me was to have the client locate the host player GameObject, then call Command and ClientRpc methods via the Host in order to simulate the Host leaving the game and thus ending the game for everybody. This was the only full proof method for the Client to be able to successfully leave the current networked multiplayer game (but not quit the app), then join a new networked game. I know this is not an ideal solution, but it works 100% of the time.

What do you mean by “simulate the Host leaving the game and thus ending the game for everybody”?
Wouldn’t that actually disconnect everyone?

This thread was started in 2016. UNet doesn’t exist anymore :stuck_out_tongue: