diff --git a/com.unity.netcode.gameobjects/CHANGELOG.md b/com.unity.netcode.gameobjects/CHANGELOG.md index afffda00bc..f0c0cd8abc 100644 --- a/com.unity.netcode.gameobjects/CHANGELOG.md +++ b/com.unity.netcode.gameobjects/CHANGELOG.md @@ -13,6 +13,7 @@ Additional documentation and release notes are available at [Multiplayer Documen - Fixed ClientRpcs always reporting in the profiler view as going to all clients, even when limited to a subset of clients by ClientRpcParams. (#2144) - Fixed RPC codegen failing to choose the correct extension methods for FastBufferReader and FastBufferWriter when the parameters were a generic type (i.e., List) and extensions for multiple instantiations of that type have been defined (i.e., List and List) (#2142) +- Implicit conversion of NetworkObjectReference to GameObject will now return null instead of throwing an exception if the referenced object could not be found (i.e., was already despawned) (#2158) - Fixed throwing an exception in OnNetworkUpdate causing other OnNetworkUpdate calls to not be executed. (#1739) - Fixed warning resulting from a stray NetworkAnimator.meta file (#2153) diff --git a/com.unity.netcode.gameobjects/Runtime/Serialization/NetworkObjectReference.cs b/com.unity.netcode.gameobjects/Runtime/Serialization/NetworkObjectReference.cs index 11a2ab2a38..263403589e 100644 --- a/com.unity.netcode.gameobjects/Runtime/Serialization/NetworkObjectReference.cs +++ b/com.unity.netcode.gameobjects/Runtime/Serialization/NetworkObjectReference.cs @@ -139,7 +139,16 @@ public void NetworkSerialize(BufferSerializer serializer) where T : IReade /// /// The to convert from. /// This returns the that the is attached to and is referenced by the passed in as a parameter - public static implicit operator GameObject(NetworkObjectReference networkObjectRef) => Resolve(networkObjectRef).gameObject; + public static implicit operator GameObject(NetworkObjectReference networkObjectRef) + { + var networkObject = Resolve(networkObjectRef); + if (networkObject != null) + { + return networkObject.gameObject; + } + + return null; + } /// /// Implicitly convert to . diff --git a/com.unity.netcode.gameobjects/Tests/Runtime/Serialization/NetworkObjectReferenceTests.cs b/com.unity.netcode.gameobjects/Tests/Runtime/Serialization/NetworkObjectReferenceTests.cs index 49c4a1f059..07cd5769c7 100644 --- a/com.unity.netcode.gameobjects/Tests/Runtime/Serialization/NetworkObjectReferenceTests.cs +++ b/com.unity.netcode.gameobjects/Tests/Runtime/Serialization/NetworkObjectReferenceTests.cs @@ -110,6 +110,32 @@ public void TestSerializeGameObject() } } + [Test] + public void TestImplicitConversionToGameObject() + { + using var networkObjectContext = UnityObjectContext.CreateNetworkObject(); + networkObjectContext.Object.Spawn(); + + NetworkObjectReference outReference = networkObjectContext.Object.gameObject; + + GameObject go = outReference; + Assert.AreEqual(networkObjectContext.Object.gameObject, go); + } + + [Test] + public void TestImplicitToGameObjectIsNullWhenNotFound() + { + using var networkObjectContext = UnityObjectContext.CreateNetworkObject(); + networkObjectContext.Object.Spawn(); + + NetworkObjectReference outReference = networkObjectContext.Object.gameObject; + + networkObjectContext.Object.Despawn(); + Object.DestroyImmediate(networkObjectContext.Object.gameObject); + + GameObject go = outReference; + Assert.IsNull(go); + } [Test] public void TestTryGet() {