Skip to content

Commit f063141

Browse files
fix: Dynamically spawned NetworkObject throws exception due to no scene of origin handle [MTT-3852] (#2017)
1 parent 08e09c2 commit f063141

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
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 dynamically spawned `NetworkObject`s could throw an exception if the scene of origin handle was zero (0) and the `NetworkObject` was already spawned. (#2017)
2324
- Fixed issue where `NetworkObject.Observers` was not being cleared when despawned. (#2009)
2425
- Fixed `NetworkAnimator` could not run in the server authoritative mode. (#2003)
2526
- Fixed issue where late joining clients would get a soft synchronization error if any in-scene placed NetworkObjects were parented under another `NetworkObject`. (#1985)

com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,7 @@ internal Scene SceneOrigin
221221
/// </summary>
222222
internal int GetSceneOriginHandle()
223223
{
224-
// Sanity check to make sure nothing
225-
if (SceneOriginHandle == 0 && IsSpawned)
224+
if (SceneOriginHandle == 0 && IsSpawned && IsSceneObject != false)
226225
{
227226
throw new Exception($"{nameof(GetSceneOriginHandle)} called when {nameof(SceneOriginHandle)} is still zero but the {nameof(NetworkObject)} is already spawned!");
228227
}

com.unity.netcode.gameobjects/Tests/Runtime/NetworkObject/NetworkObjectOnSpawnTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,23 @@ bool HasConditionBeenMet()
168168
Assert.False(s_GlobalTimeoutHelper.TimedOut, "Timed out while waiting for client side despawns! (2nd pass)");
169169
}
170170

171+
[Test]
172+
public void DynamicallySpawnedNoSceneOriginException()
173+
{
174+
var gameObject = new GameObject();
175+
var networkObject = gameObject.AddComponent<NetworkObject>();
176+
networkObject.IsSpawned = true;
177+
networkObject.SceneOriginHandle = 0;
178+
networkObject.IsSceneObject = false;
179+
// This validates invoking GetSceneOriginHandle will not throw an exception for a dynamically spawned NetworkObject
180+
// when the scene of origin handle is zero
181+
var sceneOriginHandle = networkObject.GetSceneOriginHandle();
182+
183+
// This validates that GetSceneOriginHandle will return the GameObject's scene handle that should be the currently active scene
184+
var activeSceneHandle = UnityEngine.SceneManagement.SceneManager.GetActiveScene().handle;
185+
Assert.IsTrue(sceneOriginHandle == activeSceneHandle, $"{nameof(NetworkObject)} should have returned the active scene handle of {activeSceneHandle} but returned {sceneOriginHandle}");
186+
}
187+
171188
private class TrackOnSpawnFunctions : NetworkBehaviour
172189
{
173190
public int OnNetworkSpawnCalledCount { get; private set; }

0 commit comments

Comments
 (0)