Skip to content

Commit 0c8161e

Browse files
fix: deactivated child NetworkBehaviour exceptions [NCCBUG-138] (#2096)
1 parent 0e84dcd commit 0c8161e

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ Additional documentation and release notes are available at [Multiplayer Documen
1111

1212
- When using `UnityTransport`, _reliable_ payloads are now allowed to exceed the configured 'Max Payload Size'. Unreliable payloads remain bounded by this setting. (#2081)
1313

14+
1415
### Fixed
16+
17+
- Fixed issue when attempting to spawn a parent `GameObject`, with `NetworkObject` component attached, that has one or more child `GameObject`s, that are inactive in the hierarchy, with `NetworkBehaviour` components it will no longer attempt to spawn the associated NetworkBehaviour(s) or invoke ownership changed notifications but will log a warning message. (#2096)
1518
- Fixed issue where `NetworkObject.NetworkHide` was despawning and destroying, as opposed to only despawning, in-scene placed `NetworkObject`s. (#2086)
1619
- Fixed issue where `NetworkAnimator` would not synchronize a looping animation for late joining clients if it was at the very end of its loop. (#2076)
1720
- Fixed issue where `NetworkAnimator` was not removing its subscription from `OnClientConnectedCallback` when despawned during the shutdown sequence. (#2074)

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,14 @@ internal void InvokeBehaviourOnGainedOwnership()
553553

554554
for (int i = 0; i < ChildNetworkBehaviours.Count; i++)
555555
{
556-
ChildNetworkBehaviours[i].InternalOnGainedOwnership();
556+
if (ChildNetworkBehaviours[i].gameObject.activeInHierarchy)
557+
{
558+
ChildNetworkBehaviours[i].InternalOnGainedOwnership();
559+
}
560+
else
561+
{
562+
Debug.LogWarning($"{ChildNetworkBehaviours[i].gameObject.name} is disabled! Netcode for GameObjects does not support disabled NetworkBehaviours! The {ChildNetworkBehaviours[i].GetType().Name} component was skipped during ownership assignment!");
563+
}
557564
}
558565
}
559566

@@ -805,7 +812,14 @@ internal void InvokeBehaviourNetworkSpawn()
805812

806813
for (int i = 0; i < ChildNetworkBehaviours.Count; i++)
807814
{
808-
ChildNetworkBehaviours[i].InternalOnNetworkSpawn();
815+
if (ChildNetworkBehaviours[i].gameObject.activeInHierarchy)
816+
{
817+
ChildNetworkBehaviours[i].InternalOnNetworkSpawn();
818+
}
819+
else
820+
{
821+
Debug.LogWarning($"{ChildNetworkBehaviours[i].gameObject.name} is disabled! Netcode for GameObjects does not support spawning disabled NetworkBehaviours! The {ChildNetworkBehaviours[i].GetType().Name} component was skipped during spawn!");
822+
}
809823
}
810824
}
811825

com.unity.netcode.gameobjects/Tests/Runtime/NetworkBehaviourGenericTests.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using UnityEngine;
33
using UnityEngine.TestTools;
44
using Unity.Netcode.TestHelpers.Runtime;
5+
using Unity.Netcode.Components;
56

67
namespace Unity.Netcode.RuntimeTests
78
{
@@ -23,6 +24,46 @@ public class SimpleNetworkBehaviour : NetworkBehaviour
2324
{
2425
}
2526

27+
protected override IEnumerator OnSetup()
28+
{
29+
m_AllowServerToStart = false;
30+
return base.OnSetup();
31+
}
32+
33+
/// <summary>
34+
/// This validates the fix for when a child GameObject with a NetworkBehaviour
35+
/// is deleted while the parent GameObject with a NetworkObject is spawned and
36+
/// is not deleted until a later time would cause an exception due to the
37+
/// NetworkBehaviour not being removed from the NetworkObject.ChildNetworkBehaviours
38+
/// list.
39+
/// </summary>
40+
[UnityTest]
41+
public IEnumerator ValidatedDisableddNetworkBehaviourWarning()
42+
{
43+
m_AllowServerToStart = true;
44+
45+
yield return s_DefaultWaitForTick;
46+
47+
// Now just start the Host
48+
yield return StartServerAndClients();
49+
50+
var parentObject = new GameObject();
51+
var childObject = new GameObject();
52+
childObject.name = "ChildObject";
53+
childObject.transform.parent = parentObject.transform;
54+
var parentNetworkObject = parentObject.AddComponent<NetworkObject>();
55+
var childBehaviour = childObject.AddComponent<NetworkTransform>();
56+
57+
// Set the child object to be inactive in the hierarchy
58+
childObject.SetActive(false);
59+
60+
LogAssert.Expect(LogType.Warning, $"{childObject.name} is disabled! Netcode for GameObjects does not support disabled NetworkBehaviours! The {childBehaviour.GetType().Name} component was skipped during ownership assignment!");
61+
LogAssert.Expect(LogType.Warning, $"{childObject.name} is disabled! Netcode for GameObjects does not support spawning disabled NetworkBehaviours! The {childBehaviour.GetType().Name} component was skipped during spawn!");
62+
63+
parentNetworkObject.Spawn();
64+
yield return s_DefaultWaitForTick;
65+
}
66+
2667
/// <summary>
2768
/// This test validates a fix to NetworkBehaviour.NetworkObject when
2869
/// the NetworkManager.LogLevel is set to Developer

0 commit comments

Comments
 (0)