- Home /
[Mirror Networking] NullReferenceException on player when trying to start server a second time
Hi there! I'm currently working on an online multiplayer game using Mirror. The game works perfectly on the first try when a host starts a server and the clients connect.
However, after playing for a bit and the host decides to disconnect to the main menu, when it goes to start the server and play again (without closing the game) the game breaks.
The errors that appear and break the game are all to do with getting a NullReferenceException on the PlayerScript (which is a NetworkBehaviour on each client), every time "gameObject" is called. This is strange because in my eyes gameObject should always be accessible, because if it were null the script that called it couldn't even be able to run in the first place, right?
I've tried looking online but there isn't much documentation on how to properly stop and reset a Mirror server, and I'm baffled as to why this is happening.
I'm using NetworkServer.Shutdown() to stop the server.
Any help greatly appreciated!
Answer by thealexguy1 · Jun 11, 2020 at 09:09 PM
So I fixed it. Turns out it wasn't to do with the Networking!, but DestroyOnLoad. I had code that did SceneManager.sceneLoaded += OnSceneLoaded;
on enable, and I needed to SceneManager.sceneLoaded -= OnSceneLoaded;
when I removed its DestroyOnLoad and sent it back to the current scene.