Skip to content

Commit

Permalink
fix: isServer false in OnDestroy (#2101)
Browse files Browse the repository at this point in the history
* fix isServer false in OnDestroy

* making sure that OnDestroy doesn't call Destroy again
  • Loading branch information
James-Frowen committed Aug 6, 2020
1 parent a124f3f commit d46469a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
8 changes: 7 additions & 1 deletion Assets/Mirror/Runtime/NetworkIdentity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@ public sealed class NetworkIdentity : MonoBehaviour
[FormerlySerializedAs("m_ServerOnly")]
public bool serverOnly;

/// <summary>
/// Set to try before Destroy is called so that OnDestroy doesn't try to destroy the object again
/// </summary>
internal bool destroyCalled;

/// <summary>
/// The NetworkConnection associated with this NetworkIdentity. This is only valid for player objects on a local client.
/// </summary>
Expand Down Expand Up @@ -705,7 +710,8 @@ void OnDestroy()

// If false the object has already been unspawned
// if it is still true, then we need to unspawn it
if (isServer)
// if destroy is already called don't call it again
if (isServer && !destroyCalled)
{
// Do not add logging to this (see above)
NetworkServer.Destroy(gameObject);
Expand Down
8 changes: 7 additions & 1 deletion Assets/Mirror/Runtime/NetworkServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1247,9 +1247,15 @@ static void DestroyObject(NetworkIdentity identity, bool destroyServerObject)
// when unspawning, dont destroy the server's object
if (destroyServerObject)
{
identity.destroyCalled = true;
UnityEngine.Object.Destroy(identity.gameObject);
}
identity.Reset();
// if we are destroying the server object we don't need to reset the identity
// reseting it will cause isClient/isServer to be false in the OnDestroy call
else
{
identity.Reset();
}
}

/// <summary>
Expand Down

0 comments on commit d46469a

Please sign in to comment.