Skip to content

fix: Adds a null check for NetworkObjectReference => GameObject conversion [NCCBUG-172] #2158

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
1 change: 1 addition & 0 deletions com.unity.netcode.gameobjects/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>) and extensions for multiple instantiations of that type have been defined (i.e., List<int> and List<string>) (#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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,16 @@ public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReade
/// </summary>
/// <param name="networkObjectRef">The <see cref="NetworkObjectReference"/> to convert from.</param>
/// <returns>This returns the <see cref="GameObject"/> that the <see cref="NetworkObject"/> is attached to and is referenced by the <see cref="NetworkObjectReference"/> passed in as a parameter</returns>
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;
}

/// <summary>
/// Implicitly convert <see cref="GameObject"/> to <see cref="NetworkObject"/>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down