How do you stop a client/host in Unity Netcode for Gameobjects?
Asked Answered
D

2

5

I managed to get some kind of multiplayer working with Unity Netcode which I understand is very new, but I'm running into a pretty big issue. I can't seem to be able to disconnect from the server instance. This causes an issue in which I load the game scene and the NetworkManager instance is spawned. Then I return to the main menu scene and load the game scene again, and now there are 2 NetworkManager instances which obviously causes errors.

I read in the docs that you can use this:

public void Disconnect()
{
    if (IsHost) 
    {
        NetworkManager.Singleton.StopHost();
    }
    else if (IsClient) 
    {
        NetworkManager.Singleton.StopClient();
    }
    else if (IsServer) 
    {
        NetworkManager.Singleton.StopServer();
    }
    
    UnityEngine.SceneManagement.SceneManager.LoadScene("MainMenu");
}

However all these stop functions don't seem to exist at all under the singleton. Am I missing a library or something? For now I am using a workaround which is just disabling "Don't destroy" and then the NetworkManager is destroyed after switching scenes, but what if I wanted to keep it after switching a scene? Is there no way to control when the Server/Client is stopped? I'm assuming this is a bug but I just want to make sure.

Disloyalty answered 6/12, 2021 at 10:45 Comment(5)
and now there are 2 NetworkManager instances ... if the singleton pattern is implemented correctly it would/should avoid this .. that's the whole purpose of "Singleton" ..Maracaibo
I understand this, that's why this is so weird... and I'm guessing that's what's causing all the errors. It probably needs to be shut down manually in the code, and that's what I'm trying to figure out how to do.Disloyalty
well that NetworkManager is it something you implemented or where exactly does it come from?Maracaibo
It comes from the Unity Netcode for Gameobjects API. Not something I implemented. That's why I'm surprised the functions are missing, I didn't make them.Disloyalty
docs-multiplayer.unity3d.com/docs/components/networkmanagerDisloyalty
D
10
void Disconnect()
{
    NetworkManager.Singleton.Shutdown();
}

Disconnects clients if connected and stops server if running. To answer your full question, you also need a Cleanup() function in your non-networked scene that checks for astray NetworkManager singletons and destroys them. This will avoid unnecessary duplication of network managers.

void Cleanup()
{
    if (NetworkManager.Singleton != null)
    {
        Destroy(NetworkManager.Singleton.gameObject);
    }
}
Desta answered 7/12, 2021 at 19:44 Comment(0)
M
0

Pause execution while shutting down the server to make sure it gets shutdown before proceeding

NetworkManager.Singleton.Shutdown();
while (NetworkManager.Singleton.ShutdownInProgress);

Or you can use a bool which is set to true once either the client or server has shutdown.

Edit: This doesn't work. Like Miguel said, it gets stuck.

Makassar answered 28/8, 2023 at 10:35 Comment(1)
This results in an infinte loop and application crash. I think it is due to the while loop locking the main thread and thus making the NetworkManager unable to properly shutdown. Use an async method or coroutine instead.Underset

© 2022 - 2024 — McMap. All rights reserved.