Skip to content

Commit 79f9702

Browse files
fix
Use the GameObject.activeInHierarchy fix. Allow disabled NetworkBehaviour with a disabled GameObject to internally start pre-spawn but exit before invoking protected method.
1 parent 2e68321 commit 79f9702

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,13 @@ internal void NetworkPreSpawn(ref NetworkManager networkManager, NetworkObject n
761761

762762
UpdateNetworkProperties();
763763

764+
// Exit early for disabled NetworkBehaviours.
765+
// We still want the above values to be set.
766+
if (!gameObject.activeInHierarchy)
767+
{
768+
return;
769+
}
770+
764771
try
765772
{
766773
OnNetworkPreSpawn(ref networkManager);

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

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2082,6 +2082,12 @@ internal void InvokeBehaviourOnNetworkObjectParentChanged(NetworkObject parentNe
20822082
{
20832083
for (int i = 0; i < ChildNetworkBehaviours.Count; i++)
20842084
{
2085+
// Any NetworkBehaviour that is not spawned and the associated GameObject is disabled should be
2086+
// skipped over (i.e. not supported).
2087+
if (!ChildNetworkBehaviours[i].IsSpawned && !ChildNetworkBehaviours[i].gameObject.activeInHierarchy)
2088+
{
2089+
continue;
2090+
}
20852091
// Invoke internal notification
20862092
ChildNetworkBehaviours[i].InternalOnNetworkObjectParentChanged(parentNetworkObject);
20872093
// Invoke public notification
@@ -2217,7 +2223,6 @@ public bool TrySetParent(NetworkObject parent, bool worldPositionStays = true)
22172223
// DANGO-TODO: Do we want to worry about ownership permissions here?
22182224
// It wouldn't make sense to not allow parenting, but keeping this note here as a reminder.
22192225
var isAuthority = HasAuthority || (AllowOwnerToParent && IsOwner);
2220-
Debug.Log($"something is broken! isAuthority={isAuthority} | HasAuthority={HasAuthority} | (AllowOwnerToParent && IsOwner)={(AllowOwnerToParent && IsOwner)}");
22212226

22222227
// If we don't have authority and we are not shutting down, then don't allow any parenting.
22232228
// If we are shutting down and don't have authority then allow it.
@@ -2543,10 +2548,7 @@ internal void InvokeBehaviourNetworkPreSpawn()
25432548
var networkManager = NetworkManager;
25442549
for (int i = 0; i < ChildNetworkBehaviours.Count; i++)
25452550
{
2546-
if (ChildNetworkBehaviours[i].gameObject.activeInHierarchy)
2547-
{
2548-
ChildNetworkBehaviours[i].NetworkPreSpawn(ref networkManager, this);
2549-
}
2551+
ChildNetworkBehaviours[i].NetworkPreSpawn(ref networkManager, this);
25502552
}
25512553
}
25522554

@@ -2558,10 +2560,9 @@ internal void InvokeBehaviourNetworkSpawn()
25582560
{
25592561
if (!childBehaviour.gameObject.activeInHierarchy)
25602562
{
2561-
Debug.LogWarning($"{childBehaviour.gameObject.name} is disabled! Netcode for GameObjects does not support spawning disabled NetworkBehaviours! The {childBehaviour.GetType().Name} component was skipped during spawn!");
2563+
Debug.LogWarning($"{GenerateDisabledNetworkBehaviourWarning(childBehaviour)}");
25622564
continue;
25632565
}
2564-
25652566
childBehaviour.InternalOnNetworkSpawn();
25662567
}
25672568
}
@@ -2621,7 +2622,7 @@ internal void InvokeBehaviourNetworkDespawn()
26212622

26222623
internal string GenerateDisabledNetworkBehaviourWarning(NetworkBehaviour networkBehaviour)
26232624
{
2624-
return $"[{name}][{networkBehaviour.GetType().Name}][{nameof(isActiveAndEnabled)}: {networkBehaviour.isActiveAndEnabled}] Disabled {nameof(NetworkBehaviour)}s are not supported and will be excluded from spawning and synchronization!";
2625+
return $"[{name}][{networkBehaviour.GetType().Name}][{nameof(isActiveAndEnabled)}: {networkBehaviour.isActiveAndEnabled}] Disabled {nameof(NetworkBehaviour)}s will be excluded from spawning and synchronization!";
26252626
}
26262627

26272628
internal List<NetworkBehaviour> ChildNetworkBehaviours
@@ -2644,11 +2645,6 @@ internal List<NetworkBehaviour> ChildNetworkBehaviours
26442645
{
26452646
continue;
26462647
}
2647-
else if (!networkBehaviours[i].isActiveAndEnabled)
2648-
{
2649-
Debug.LogWarning(GenerateDisabledNetworkBehaviourWarning(networkBehaviours[i]));
2650-
continue;
2651-
}
26522648

26532649
// Set ourselves as the NetworkObject that this behaviour belongs to and add it to the child list
26542650
var nextIndex = (ushort)m_ChildNetworkBehaviours.Count;

0 commit comments

Comments
 (0)