fix: NetworkManager.StopServer/StopClient: avoid NullReferenceException when called in OnApplicationQuit or from tests

This commit is contained in:
vis2k 2021-03-06 14:04:37 +08:00
parent 425e3131dc
commit 25ece5a0d5

View File

@ -646,7 +646,10 @@ public void StopServer()
// Get Network Manager out of DDOL before going to offline scene
// to avoid collision and let a fresh Network Manager be created.
if (gameObject.scene.name == "DontDestroyOnLoad"
// IMPORTANT: .gameObject can be null if StopClient is called from
// OnApplicationQuit or from tests!
if (gameObject != null &&
gameObject.scene.name == "DontDestroyOnLoad"
&& !string.IsNullOrEmpty(offlineScene)
&& SceneManager.GetActiveScene().path != offlineScene)
SceneManager.MoveGameObjectToScene(gameObject, SceneManager.GetActiveScene());
@ -684,7 +687,10 @@ public void StopClient()
// Get Network Manager out of DDOL before going to offline scene
// to avoid collision and let a fresh Network Manager be created.
if (gameObject.scene.name == "DontDestroyOnLoad"
// IMPORTANT: .gameObject can be null if StopClient is called from
// OnApplicationQuit or from tests!
if (gameObject != null &&
gameObject.scene.name == "DontDestroyOnLoad"
&& !string.IsNullOrEmpty(offlineScene)
&& SceneManager.GetActiveScene().path != offlineScene)
SceneManager.MoveGameObjectToScene(gameObject, SceneManager.GetActiveScene());