Skip to content

Commit

Permalink
fix: #2954 calling StopClient in host mode does not destroy other cli…
Browse files Browse the repository at this point in the history
…ent's objects anymore
  • Loading branch information
miwarnec committed Mar 23, 2022
1 parent 6f98122 commit d8774ec
Showing 1 changed file with 27 additions and 15 deletions.
42 changes: 27 additions & 15 deletions Assets/Mirror/Runtime/NetworkClient.cs
Expand Up @@ -1432,27 +1432,39 @@ public static void DestroyAllClientObjects()

identity.OnStopClient();

bool wasUnspawned = InvokeUnSpawnHandler(identity.assetId, identity.gameObject);

// unspawned objects should be reset for reuse later.
if (wasUnspawned)
{
identity.Reset();
}
// without unspawn handler, we need to disable/destroy.
else
// NetworkClient.Shutdown calls DestroyAllClientObjects.
// which destroys all objects in NetworkClient.spawned.
// => NC.spawned contains owned & observed objects
// => in host mode, we CAN NOT destroy observed objects.
// => that would destroy them other connection's objects
// on the host server, making them disconnect.
// https://github.com/vis2k/Mirror/issues/2954
bool hostOwned = identity.connectionToServer is LocalConnectionToServer;
bool shouldDestroy = !identity.isServer || hostOwned;
if (shouldDestroy)
{
// scene objects are reset and disabled.
// they always stay in the scene, we don't destroy them.
if (identity.sceneId != 0)
bool wasUnspawned = InvokeUnSpawnHandler(identity.assetId, identity.gameObject);

// unspawned objects should be reset for reuse later.
if (wasUnspawned)
{
identity.Reset();
identity.gameObject.SetActive(false);
}
// spawned objects are destroyed
// without unspawn handler, we need to disable/destroy.
else
{
GameObject.Destroy(identity.gameObject);
// scene objects are reset and disabled.
// they always stay in the scene, we don't destroy them.
if (identity.sceneId != 0)
{
identity.Reset();
identity.gameObject.SetActive(false);
}
// spawned objects are destroyed
else
{
GameObject.Destroy(identity.gameObject);
}
}
}
}
Expand Down

0 comments on commit d8774ec

Please sign in to comment.