Skip to content

Commit 72b3f4f

Browse files
committed
Untangle NetworkObject.OnDestroy
1 parent 42ce1f1 commit 72b3f4f

File tree

1 file changed

+24
-30
lines changed

1 file changed

+24
-30
lines changed

com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1750,43 +1750,37 @@ private void OnDestroy()
17501750
return;
17511751
}
17521752

1753-
// An authorized destroy is when done by the authority instance or done due to a scene event and the NetworkObject
1754-
// was marked as destroy pending scene event (which means the destroy with scene property was set).
1755-
if (IsSpawned && !(HasAuthority || networkManager.DAHost || DestroyPendingSceneEvent) && networkManager.IsListening &&
1756-
(IsSceneObject == null || IsSceneObject.Value != true))
1757-
{
1758-
// If we destroyed a GameObject with a NetworkObject component on the non-authority side, handle cleaning up the SceneMigrationSynchronization.
1759-
networkManager.SpawnManager?.RemoveNetworkObjectFromSceneChangedUpdates(this);
1760-
1761-
// Clients should not despawn NetworkObjects while connected to a session, but we don't want to destroy the current call stack
1762-
// if this happens. Instead, we should just generate a network log error and exit early (as long as we are not shutting down).
1763-
if (!networkManager.ShutdownInProgress)
1764-
{
1765-
// Since we still have a session connection, log locally and on the server to inform user of this issue.
1766-
// If the NetworkObject's GameObject is not valid or the scene is no longer valid or loaded, then this was due to the
1767-
// unloading of a scene which is done by the authority...
1768-
if (gameObject != null && gameObject.scene.IsValid() && gameObject.scene.isLoaded)
1753+
// Always attempt to remove from scene changed updates
1754+
networkManager.SpawnManager?.RemoveNetworkObjectFromSceneChangedUpdates(this);
1755+
1756+
if (IsSpawned && !networkManager.ShutdownInProgress)
1757+
{
1758+
// An authorized destroy is when done by the authority instance or done due to a scene event and the NetworkObject
1759+
// was marked as destroy pending scene event (which means the destroy with scene property was set).
1760+
var isAuthorityDestroy = HasAuthority || NetworkManager.DAHost || DestroyPendingSceneEvent;
1761+
1762+
// If the NetworkObject's GameObject is still valid and the scene is still valid and loaded, then we are still valid
1763+
var isStillValid = gameObject != null && gameObject.scene.IsValid() && gameObject.scene.isLoaded;
1764+
1765+
// If we're not the authority and everything is valid and dynamically spawned, then the destroy is not valid.
1766+
if (!isAuthorityDestroy && IsSceneObject == false && isStillValid)
1767+
{
1768+
if (networkManager.LogLevel <= LogLevel.Error)
17691769
{
1770-
if (networkManager.LogLevel <= LogLevel.Error)
1770+
if (networkManager.DistributedAuthorityMode)
17711771
{
1772-
if (networkManager.DistributedAuthorityMode)
1773-
{
1774-
NetworkLog.LogError($"[Invalid Destroy][{gameObject.name}][NetworkObjectId:{NetworkObjectId}] Destroy a spawned {nameof(NetworkObject)} on a non-owner client is not valid during a distributed authority session. Call {nameof(Destroy)} or {nameof(Despawn)} on the client-owner instead.");
1775-
}
1776-
else
1777-
{
1778-
NetworkLog.LogErrorServer($"[Invalid Destroy][{gameObject.name}][NetworkObjectId:{NetworkObjectId}] Destroy a spawned {nameof(NetworkObject)} on a non-host client is not valid. Call {nameof(Destroy)} or {nameof(Despawn)} on the server/host instead.");
1779-
}
1772+
NetworkLog.LogError($"[Invalid Destroy][{gameObject.name}][NetworkObjectId:{NetworkObjectId}] Destroy a spawned {nameof(NetworkObject)} on a non-owner client is not valid during a distributed authority session. Call {nameof(Destroy)} or {nameof(Despawn)} on the client-owner instead.");
1773+
}
1774+
else
1775+
{
1776+
NetworkLog.LogErrorServer($"[Invalid Destroy][{gameObject.name}][NetworkObjectId:{NetworkObjectId}] Destroy a spawned {nameof(NetworkObject)} on a non-host client is not valid. Call {nameof(Destroy)} or {nameof(Despawn)} on the server/host instead.");
17801777
}
1781-
return;
17821778
}
1779+
1780+
return;
17831781
}
1784-
// Otherwise, clients can despawn NetworkObjects while shutting down and should not generate any messages when this happens
17851782
}
17861783

1787-
// Always attempt to remove from scene changed updates
1788-
networkManager.SpawnManager?.RemoveNetworkObjectFromSceneChangedUpdates(this);
1789-
17901784
if (networkManager.SpawnManager != null && networkManager.SpawnManager.SpawnedObjects.TryGetValue(NetworkObjectId, out var networkObject))
17911785
{
17921786
if (this == networkObject)

0 commit comments

Comments
 (0)