Bug: NetworkManager's OnServerDisconnect function is never called for Host

I attached a simple project with 13 lines of code that shows how NetworkManager:OnServerDisconnect is never called when the host disconnects. This is a problem because often game servers need to save player data after the player quits the game. This works fine for clients that connect to the server, because OnServerDisconnect is called when those clients quit the game.

It is however not called when I run the server as host (aka I also play on it) and then stop the server (either with the Unity Editor stop button, or with the GUI stop button in the NetworkManagerHud). I think it should be called because we need some kind of “onquit” function to save the host data too. OnServerDisconnect would be perfect for this because it happens before “OnDestroy”, hence we can be 100% sure that the player objects hasn’t been destroyed yet and can still be saved.

How to reproduce:

  • Open the Project, open the Scene
  • Press Play
  • Select “Start Host”
  • Stop it
  • See how there is no log message because NetworkManagerCustom:OnServerDisconnect is not called

[Tested on Unity 5.3.1f1]

2465117–169640–networkmanager onserverdisconnect not called when quitting the editor.zip (37.8 KB)

StopHost does not trigger OnServerDisconnect… it shuts down the server and client, there is no disconnect event.

Does that mean I have to code seperately what happens once the Client or Host disconnects?

Hi there,
how did you solve the issue for Host-Quit?

I have to save every few seconds and hope for the best. I still haven’t found any better way.

If you are Server, it will not call ServerDisconnect. But if Client will disconnect it Will call on Server.