Skip to content

Commit b9946cd

Browse files
fix: NetworkBehaviourReference throws type case exception when using TryGet [NCCBUG-167] (#1984)
* fix NCCBUG-167 This fixes the issue with the type (T) casting throwing an exception when the type is not found (i.e. trying to cast a type to null will asset...trying to get the result of getting a type as (T) will still return null. * update adding the changelog entry * test This test verifies the fix. * update adding PR number to the change log entry
1 parent 75c646a commit b9946cd

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

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

2222
### Fixed
23+
- Fixed issue where `NetworkBehaviourReference` would throw a type cast exception if using `NetworkBehaviourReference.TryGet` and the component type was not found. (#1984)
2324
- Fixed issue where one or more clients disconnecting during a scene event would cause `LoadEventCompleted` or `UnloadEventCompleted` to wait until the `NetworkConfig.LoadSceneTimeOut` period before being triggered. (#1973)
2425
- Fixed issues when multiple `ConnectionApprovalCallback`s were registered (#1972)
2526
- `FixedString` types can now be used in NetworkVariables and RPCs again without requiring a `ForceNetworkSerializeByMemcpy<>` wrapper (#1961)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public bool TryGet(out NetworkBehaviour networkBehaviour, NetworkManager network
5353
/// <returns>True if the <see cref="NetworkBehaviour"/> was found; False if the <see cref="NetworkBehaviour"/> was not found. This can happen if the corresponding <see cref="NetworkObject"/> has not been spawned yet. you can try getting the reference at a later point in time.</returns>
5454
public bool TryGet<T>(out T networkBehaviour, NetworkManager networkManager = null) where T : NetworkBehaviour
5555
{
56-
networkBehaviour = (T)GetInternal(this, null);
56+
networkBehaviour = GetInternal(this, null) as T;
5757
return networkBehaviour != null;
5858
}
5959

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ public IEnumerator TestRpc()
5757
Assert.AreEqual(testNetworkBehaviour, testNetworkBehaviour.RpcReceivedBehaviour);
5858
}
5959

60+
61+
62+
6063
[UnityTest]
6164
public IEnumerator TestRpcImplicitNetworkBehaviour()
6265
{
@@ -149,4 +152,44 @@ public NetworkBehaviourReferenceTests()
149152
NetworkManagerHelper.StartNetworkManager(out _);
150153
}
151154
}
155+
156+
/// <summary>
157+
/// Integration tests for NetworkBehaviourReference
158+
/// </summary>
159+
public class NetworkBehaviourReferenceIntegrationTests : NetcodeIntegrationTest
160+
{
161+
protected override int NumberOfClients => 1;
162+
163+
internal class FakeMissingComponent : NetworkBehaviour
164+
{
165+
166+
}
167+
168+
internal class TestAddedComponent : NetworkBehaviour
169+
{
170+
171+
}
172+
173+
protected override void OnCreatePlayerPrefab()
174+
{
175+
m_PlayerPrefab.AddComponent<TestAddedComponent>();
176+
base.OnCreatePlayerPrefab();
177+
}
178+
179+
/// <summary>
180+
/// This test validates that if a component does not exist the NetworkBehaviourReference will not throw an
181+
/// invalid cast exception.
182+
/// (It is a full integration test to assure the NetworkObjects are spawned)
183+
/// </summary>
184+
[UnityTest]
185+
public IEnumerator TestTryGetWithAndWithOutExistingComponent()
186+
{
187+
var networkBehaviourReference = new NetworkBehaviourReference(m_ClientNetworkManagers[0].LocalClient.PlayerObject.GetComponent<TestAddedComponent>());
188+
var missingComponent = (FakeMissingComponent)null;
189+
var testBehaviour = (TestAddedComponent)null;
190+
Assert.IsFalse(networkBehaviourReference.TryGet(out missingComponent));
191+
Assert.IsTrue(networkBehaviourReference.TryGet(out testBehaviour));
192+
yield return null;
193+
}
194+
}
152195
}

0 commit comments

Comments
 (0)