NetworkManager immediately disconnects from the server on pressing X on window

I have set Application.wantsToQuit to trigger a method which will display a confirmation dialog. Only when pressed Yes, will the application actually quit. But NetworkManager doesn’t seem to care about that. The moment I press X, it’s over.

I tried asking chatgpt but it doesn’t have any solution for this. Has anyone else encountered this? How did you resolve it or worked your way around it?

Here’s my code:

private bool quitConfirmed = false;

private void OnEnable() => Application.wantsToQuit += QuitConfirmBox;
private void OnDisable() => Application.wantsToQuit -= QuitConfirmBox;

public void Quit()   /* called by pressing YES when prompted */
{
    quitConfirmed = true;
    if (NetworkManager.Singleton != null) NetworkManager.Singleton.Shutdown();
    Application.Quit();
}

private bool QuitConfirmBox()
{
    uiManager.ConfirmQuitMessageBox();
    return quitConfirmed;
}

private void OnApplicationQuit()
{
}

Removing the Shutdown calll from the code didn’t make any difference.

Bruh, has no one encountered this issue?

I tried this on macOS and what I observed is that even if wantsToQuit returns false MonoBehaviour’s OnApplicationQuit is still called, even though the application won’t be shut down. I don’t know if there’s a way of preventing this happening. :man_shrugging:

You can check the NetworkManager code for OnApplicationQuit and if that does nothing but call Shutdown() then you could just comment that out.

1 Like