Skip to content

Commit 9ac9983

Browse files
authored
fix: better error message when calling RPCs on NetworkObjects that haven't been spawned yet (backport) (#1730)
1 parent fb59e49 commit 9ac9983

File tree

1 file changed

+4
-1
lines changed
  • com.unity.netcode.gameobjects/Runtime/Messaging/Messages

1 file changed

+4
-1
lines changed

com.unity.netcode.gameobjects/Runtime/Messaging/Messages/RpcMessages.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ public static unsafe bool Deserialize(ref FastBufferReader reader, ref NetworkCo
6666
public static void Handle(ref NetworkContext context, ref RpcMetadata metadata, ref FastBufferReader payload, ref __RpcParams rpcParams)
6767
{
6868
var networkManager = (NetworkManager)context.SystemOwner;
69-
var networkObject = networkManager.SpawnManager.SpawnedObjects[metadata.NetworkObjectId];
69+
if (!networkManager.SpawnManager.SpawnedObjects.TryGetValue(metadata.NetworkObjectId, out var networkObject))
70+
{
71+
throw new InvalidOperationException($"An RPC called on a {nameof(NetworkObject)} that is not in the spawned objects list. Please make sure the {nameof(NetworkObject)} is spawned before calling RPCs.");
72+
}
7073
var networkBehaviour = networkObject.GetNetworkBehaviourAtOrderIndex(metadata.NetworkBehaviourId);
7174

7275
try

0 commit comments

Comments
 (0)