Skip to content

Commit 593c2f7

Browse files
fix
This fixes the issue with invoking RPCs on other child NetworkBehaviours during OnNetworkSpawn.
1 parent f9f721a commit 593c2f7

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -779,13 +779,22 @@ internal void NetworkPreSpawn(ref NetworkManager networkManager, NetworkObject n
779779
}
780780
}
781781

782+
/// <summary>
783+
/// Handles the initialization of all child <see cref="NetworkBehaviour"/>s.
784+
/// </summary>
782785
internal void InternalOnNetworkSpawn()
783786
{
784787
IsSpawned = true;
785788
// Initialize the NetworkVariables so they are accessible in OnNetworkSpawn;
786789
InitializeVariables();
787790
UpdateNetworkProperties();
791+
}
788792

793+
/// <summary>
794+
/// Handles invoking <see cref="OnNetworkSpawn"/>.
795+
/// </summary>
796+
internal void NetworkSpawn()
797+
{
789798
try
790799
{
791800
OnNetworkSpawn();
@@ -979,12 +988,8 @@ private bool ValidateRpcMessageMetrics(Type type)
979988

980989
if (!__rpc_name_table.ContainsKey(type))
981990
{
982-
__initializeRpcs();
983-
if (!__rpc_name_table.ContainsKey(type))
984-
{
985-
Debug.LogError($"[{nameof(TrackRpcMetricsSend)}] Rpc table does not contain an entry for {type.Name}! Failed to initialize RPCs for {type.Name}.");
986-
return false;
987-
}
991+
Debug.LogError($"[{nameof(TrackRpcMetricsSend)}] Rpc table does not contain an entry for {type.Name}! Failed to initialize RPCs for {type.Name}.");
992+
return false;
988993
}
989994
return true;
990995
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2556,6 +2556,8 @@ internal void InvokeBehaviourNetworkSpawn()
25562556
{
25572557
NetworkManager.SpawnManager.UpdateOwnershipTable(this, OwnerClientId);
25582558

2559+
// Always invoke all internal network spawn methods on each child NetworkBehaviour
2560+
// ** before ** invoking OnNetworkSpawn.
25592561
foreach (var childBehaviour in ChildNetworkBehaviours)
25602562
{
25612563
if (!childBehaviour.gameObject.activeInHierarchy)
@@ -2565,6 +2567,17 @@ internal void InvokeBehaviourNetworkSpawn()
25652567
}
25662568
childBehaviour.InternalOnNetworkSpawn();
25672569
}
2570+
2571+
// Invoke OnNetworkSpawn on each child NetworkBehaviour
2572+
foreach (var childBehaviour in ChildNetworkBehaviours)
2573+
{
2574+
if (!childBehaviour.gameObject.activeInHierarchy)
2575+
{
2576+
Debug.LogWarning($"{GenerateDisabledNetworkBehaviourWarning(childBehaviour)}");
2577+
continue;
2578+
}
2579+
childBehaviour.NetworkSpawn();
2580+
}
25682581
}
25692582

25702583
internal void InvokeBehaviourNetworkPostSpawn()

0 commit comments

Comments
 (0)