Skip to content

Commit 7936ea2

Browse files
ShadauxCatJesse Olmer
andauthored
fix: Adds a null check for NetworkObjectReference => GameObject conversion [NCCBUG-172] (#2158)
* fix: Adds a null check for NetworkObjectReference => GameObject conversion [NCCBUG-172] * Changelog * style * Update com.unity.netcode.gameobjects/Tests/Runtime/Serialization/NetworkObjectReferenceTests.cs Co-authored-by: Jesse Olmer <jesseo@unity3d.com> * Review feedback. Co-authored-by: Jesse Olmer <jesseo@unity3d.com>
1 parent 8a34a1c commit 7936ea2

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
1313

1414
- Fixed ClientRpcs always reporting in the profiler view as going to all clients, even when limited to a subset of clients by ClientRpcParams. (#2144)
1515
- 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)
16+
- 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)
1617
- Fixed throwing an exception in OnNetworkUpdate causing other OnNetworkUpdate calls to not be executed. (#1739)
1718
- Fixed warning resulting from a stray NetworkAnimator.meta file (#2153)
1819

com.unity.netcode.gameobjects/Runtime/Serialization/NetworkObjectReference.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,16 @@ public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReade
139139
/// </summary>
140140
/// <param name="networkObjectRef">The <see cref="NetworkObjectReference"/> to convert from.</param>
141141
/// <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>
142-
public static implicit operator GameObject(NetworkObjectReference networkObjectRef) => Resolve(networkObjectRef).gameObject;
142+
public static implicit operator GameObject(NetworkObjectReference networkObjectRef)
143+
{
144+
var networkObject = Resolve(networkObjectRef);
145+
if (networkObject != null)
146+
{
147+
return networkObject.gameObject;
148+
}
149+
150+
return null;
151+
}
143152

144153
/// <summary>
145154
/// Implicitly convert <see cref="GameObject"/> to <see cref="NetworkObject"/>.

com.unity.netcode.gameobjects/Tests/Runtime/Serialization/NetworkObjectReferenceTests.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,32 @@ public void TestSerializeGameObject()
110110
}
111111
}
112112

113+
[Test]
114+
public void TestImplicitConversionToGameObject()
115+
{
116+
using var networkObjectContext = UnityObjectContext.CreateNetworkObject();
117+
networkObjectContext.Object.Spawn();
118+
119+
NetworkObjectReference outReference = networkObjectContext.Object.gameObject;
120+
121+
GameObject go = outReference;
122+
Assert.AreEqual(networkObjectContext.Object.gameObject, go);
123+
}
124+
125+
[Test]
126+
public void TestImplicitToGameObjectIsNullWhenNotFound()
127+
{
128+
using var networkObjectContext = UnityObjectContext.CreateNetworkObject();
129+
networkObjectContext.Object.Spawn();
130+
131+
NetworkObjectReference outReference = networkObjectContext.Object.gameObject;
132+
133+
networkObjectContext.Object.Despawn();
134+
Object.DestroyImmediate(networkObjectContext.Object.gameObject);
135+
136+
GameObject go = outReference;
137+
Assert.IsNull(go);
138+
}
113139
[Test]
114140
public void TestTryGet()
115141
{

0 commit comments

Comments
 (0)