fix: isServer false in OnDestroy (#2101)

* fix isServer false in OnDestroy

* making sure that OnDestroy doesn't call Destroy again
This commit is contained in:
James Frowen 2020-08-06 16:55:35 +01:00 committed by GitHub
parent a124f3f439
commit d46469a79c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 2 deletions

View File

@ -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>
@ -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);

View File

@ -1247,10 +1247,16 @@ 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);
}
// 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>
/// Destroys this object and corresponding objects on all clients.