Skip to content

Commit 0abc272

Browse files
fix: NetcodeIntegrationTest destroy GameObjects not NetworkObjects during shutdown sequence (#1756)
* fix Destroy the GameObject and not the NetworkObject during DestroySceneNetworkObjects * style Added comment. * fix Some scenarios the NetworkObject and/or the GameObject can be null. This update takes that into account. * refactor fix This is a cleaner and more accurate fix for the issue where a parent is destroyed before its child/children. Added additional comments to explain why.
1 parent 98fbfbf commit 0abc272

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

com.unity.netcode.gameobjects/TestHelpers/Runtime/NetcodeIntegrationTest.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,9 +493,22 @@ protected void DestroySceneNetworkObjects()
493493
var networkObjects = Object.FindObjectsOfType<NetworkObject>();
494494
foreach (var networkObject in networkObjects)
495495
{
496+
// This can sometimes be null depending upon order of operations
497+
// when dealing with parented NetworkObjects. If NetworkObjectB
498+
// is a child of NetworkObjectA and NetworkObjectA comes before
499+
// NetworkObjectB in the list of NeworkObjects found, then when
500+
// NetworkObjectA's GameObject is destroyed it will also destroy
501+
// NetworkObjectB's GameObject which will destroy NetworkObjectB.
502+
// If there is a null entry in the list, this is the most likely
503+
// scenario and so we just skip over it.
504+
if (networkObject == null)
505+
{
506+
continue;
507+
}
496508
if (CanDestroyNetworkObject(networkObject))
497509
{
498-
Object.DestroyImmediate(networkObject);
510+
// Destroy the GameObject that holds the NetworkObject component
511+
Object.DestroyImmediate(networkObject.gameObject);
499512
}
500513
}
501514
}

0 commit comments

Comments
 (0)