Skip to content

Commit 39818f2

Browse files
fix: exception thrown when rpc received for depsawned object (back-port) (#3055)
* fix We should not throw an exception if an RPC is received and the target NetworkObject does not exist as it could have been despawned. * update Adding changelog entry * update adding changelog entry PR number.
1 parent 67b27b7 commit 39818f2

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
1515

1616
### Fixed
1717

18+
- Fixed issue where an exception could occur when receiving a universal RPC for a `NetworkObject` that has been despawned. (#3055)
1819
- Fixed issue where setting a prefab hash value during connection approval but not having a player prefab assigned could cause an exception when spawning a player. (#3046)
1920
- Fixed issue where collections v2.2.x was not supported when using UTP v2.2.x within Unity v2022.3. (#3033)
2021
- Fixed issue where the `NetworkSpawnManager.HandleNetworkObjectShow` could throw an exception if one of the `NetworkObject` components to show was destroyed during the same frame. (#3029)

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

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System;
21
using Unity.Collections;
32

43
namespace Unity.Netcode
@@ -35,7 +34,13 @@ public unsafe void Handle(ref NetworkContext context)
3534
var networkManager = (NetworkManager)context.SystemOwner;
3635
if (!networkManager.SpawnManager.SpawnedObjects.TryGetValue(WrappedMessage.Metadata.NetworkObjectId, out var networkObject))
3736
{
38-
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.");
37+
// If the NetworkObject no longer exists then just log a warning when developer mode logging is enabled and exit.
38+
// This can happen if NetworkObject is despawned and a client sends an RPC before receiving the despawn message.
39+
if (networkManager.LogLevel == LogLevel.Developer)
40+
{
41+
NetworkLog.LogWarning($"[{WrappedMessage.Metadata.NetworkObjectId}, {WrappedMessage.Metadata.NetworkBehaviourId}, {WrappedMessage.Metadata.NetworkRpcMethodId}] 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.");
42+
}
43+
return;
3944
}
4045

4146
var observers = networkObject.Observers;

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,13 @@ public static void Handle(ref NetworkContext context, ref RpcMetadata metadata,
6161
var networkManager = (NetworkManager)context.SystemOwner;
6262
if (!networkManager.SpawnManager.SpawnedObjects.TryGetValue(metadata.NetworkObjectId, out var networkObject))
6363
{
64-
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.");
64+
// If the NetworkObject no longer exists then just log a warning when developer mode logging is enabled and exit.
65+
// This can happen if NetworkObject is despawned and a client sends an RPC before receiving the despawn message.
66+
if (networkManager.LogLevel == LogLevel.Developer)
67+
{
68+
NetworkLog.LogWarning($"[{metadata.NetworkObjectId}, {metadata.NetworkBehaviourId}, {metadata.NetworkRpcMethodId}] 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.");
69+
}
70+
return;
6571
}
6672
var networkBehaviour = networkObject.GetNetworkBehaviourAtOrderIndex(metadata.NetworkBehaviourId);
6773

0 commit comments

Comments
 (0)