How do I properly leave/disconnect from a multiplayer server?

Making an attempt at the nightmare that is multiplayer and I got hosting a server down, but when I leave the server, the host button no longer works saying that “Server or Client already started.” I’ve tried so many different things but It either leads to:
“Server or Client already started”
or
“Duplicate NetworkManager, deleting duplicate”

(using Mirror, SteamWorks, and probably some other things)

You’ve not stopped the server, the server is still running on that socket. What I do is

                    try{manager.StopHost();} catch{}
                    try{manager.StopClient();} catch{}

You can put various conditions in those catches, the try:catch format is so that this is ubiquitous on both server and client.

“The name ‘manager’ does not exist in the current context”

ah, I guess I should have specified: my NetworkManager is just named manager, so replace “manager” there with the name of yours (“NetworkManager.singleton”)

yeah that got rid of the red lines but now I’m back to the other error I mentioned “Multiple NetworkManagers detected in the scene. Only one NetworkManager can exist at a time. The duplicate NetworkManager will be destroyed” which replaces the current NetworkManager with a new one, which as a result completely skrews up the Host Lobby button by deleting it’s “on click()”

so I guess what I need now is a way to get the “on click()” function back to the host button after going to the main menu or something idk

(for reference this is what it looks like originally)
image

It’s better to avoid having to destroy the network manager. Instead put it in a start scene which is loaded once before the menu scene so there’s no need to load that scene again.

To find out when the network has shut down take a look at OnServerStopped/OnClientStopped, these are called very near the end of the shutdown process.

That’s just plain bad code!
You’re relying on StopHost to fail with an exception when you run it on a client. All the while this code ignores all other reasons for exceptions to be raised, leading to follow-up issues because the exceptions are simply ignored.

There has got to be some way of doing this:

if (manager.isServer)
    manager.StopHost();
else
    manager.StopClient();

“‘NetworkManager’ does not contain a definition for ‘isServer’ and no accessible extension method ‘isServer’ accepting a first argument of type ‘NetworkManager’ could be found (are you missing a using directive or an assembly reference?)”

That is correct.

The property you are looking for is IsServer. Your IDE will tell you that through its autocompletion suggestions.

Just a quick note: at the top you’re stopping the client and then doing it properly again at the bottom. Just delete line 96: NetworkManager.singleton.StopClient(); (the one right below the declaration of ReturnMainMenu())