Skip to content
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)

## [1.0.1] - 2022-08-23
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ 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) => Resolve(networkObjectRef)?.gameObject;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use null-conditional operator on MonoBehaviour's - it's not supported. You have to do the full if (ref != null) and the engine marshalls to native to verify the object hasn't been destroyed.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed. Thanks for pointing that out.


/// <summary>
/// Implicitly convert <see cref="GameObject"/> to <see cref="NetworkObject"/>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ private class TestNetworkBehaviour : NetworkBehaviour

public GameObject RpcReceivedGameObject;

public bool ReceivedRpc;
Comment thread
ShadauxCat marked this conversation as resolved.
Outdated

[ServerRpc]
public void SendReferenceServerRpc(NetworkObjectReference value)
{
ReceivedRpc = true;
RpcReceivedGameObject = value;
RpcReceivedNetworkObject = value;
}
Expand Down Expand Up @@ -110,6 +113,49 @@ public void TestSerializeGameObject()
}
}

[Test]
public void TestNoExceptionsIfObjectDisappears()
{
using var networkObjectContext = UnityObjectContext.CreateNetworkObject();
networkObjectContext.Object.Spawn();

var outWriter = new FastBufferWriter(1300, Allocator.Temp);
try
{
// serialize
var outSerializer = new BufferSerializer<BufferSerializerWriter>(new BufferSerializerWriter(outWriter));
NetworkObjectReference outReference = networkObjectContext.Object.gameObject;
outReference.NetworkSerialize(outSerializer);

networkObjectContext.Object.Despawn();
Object.DestroyImmediate(networkObjectContext.Object.gameObject);

// deserialize
NetworkObjectReference inReference = default;
var inReader = new FastBufferReader(outWriter, Allocator.Temp);
try
{
var inSerializer =
new BufferSerializer<BufferSerializerReader>(new BufferSerializerReader(inReader));
inReference.NetworkSerialize(inSerializer);
}
finally
{
inReader.Dispose();
}
GameObject gameObject = inReference;
NetworkObject networkObject = inReference;

// because the object is gone, it won't exist on the now, so it should end up null and shouldn't throw exceptions.
Assert.AreEqual(null, networkObject);
Assert.AreEqual(null, gameObject);
}
finally
{
outWriter.Dispose();
}
}

Comment thread
ShadauxCat marked this conversation as resolved.
[Test]
public void TestTryGet()
{
Expand Down