- Home /
[Unet] NetworkManager singleton auto-destruction
Hello,
I'm working with UNet and I use a custom NetworkManager inheriting from the NetworkLobbyManager. The thing is I use the same scene for the lobby and the main menu, so my offline scene is the same scene as the one where the manager is created.
So my menu scene gets reloaded when I start a server/client/host. The problem is that I have a lot of the UI linked to my manager, and since its needs to be dontDestroyOnLoad for my play_scene, I get duplicated at the reload when I stop the host/server/client The base networkManager handles the singleton by suppressing the new one, and I don't want that ! I want to keep the new one and destroy the old one, so I keep my UI linking.
I tried to do it hiding the Awake() method and destroying the other singleton but the result is that my new singleton gets destroyed as well, and I can't figure out why !
private void Awake()
{
ShutDownAndReplaceSingleton();
Debug.LogWarning("Network Awake done");
}
private void ShutDownAndReplaceSingleton()
{
Debug.LogWarning("Replacing Network singleton....");
if (singleton != null && singleton != this)
{
ShutdownSingleton();
}
singleton = this;
DontDestroyOnLoad(singleton.gameObject);
Debug.LogWarning("... new singleton id is " + singleton.GetInstanceID());
}
private void ShutdownSingleton()
{
if (singleton == null)
return;
GameObject o = singleton.gameObject;
Debug.LogWarning("Shuting down " + singleton.GetInstanceID());
Shutdown();
Destroy(o);
}
Honestly, you should really look into separating your game into an offline menu scene and an online play scene. It can solve a lot of issues, I implemented into my UNet game and it fixed disconnecting, UI, and continuity issues with little modification.
Your answer
Follow this Question
Related Questions
UNET Multiplayer Lobby not creating an instance of the lobby player 1 Answer
Multiplayer game with multiple level 0 Answers
[UNET] LobbyManager - OnLobbyServerSceneLoadedForPlayer() not executing for clients 1 Answer
OnStart ___ vs OnLobbyStart ___ methods from NetworkManager and NetworkLobbyManager 1 Answer
Unity networking tutorial? 6 Answers