Skip to content

Commit 423c9e2

Browse files
fix: spawn a NetworkObject shown after a scene migration in the server's scene
A client that starts observing a NetworkObject with NetworkShow after the NetworkObject migrated into another scene was never sent that migration, and instantiated the NetworkObject in its active scene. The client now moves a spawned NetworkObject into its server-side scene, as it already does for NetworkObjects that are part of its initial synchronization. Adds CHANGELOG entries for this and for scene migrations being sent to clients that do not observe the NetworkObject.
1 parent 2897835 commit 423c9e2

3 files changed

Lines changed: 31 additions & 0 deletions

File tree

‎com.unity.netcode.gameobjects/CHANGELOG.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ Additional documentation and release notes are available at [Multiplayer Documen
1818

1919
### Fixed
2020

21+
- Issue where moving a `NetworkObject` into another scene made the clients that did not observe it log "Trying to synchronize NetworkObjectId but it was not spawned". The scene migration is now only sent to the clients that observe the `NetworkObject`.
22+
- Issue where a `NetworkObject` that was moved into another scene while hidden from a client spawned in that client's active scene when it was shown with `NetworkShow`, instead of the scene it is in on the server.
23+
2124
### Security
2225

2326
### Obsolete

‎com.unity.netcode.gameobjects/Runtime/Messaging/Messages/CreateObjectMessage.cs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,13 @@ internal static void CreateObject(ref NetworkManager networkManager, ulong sende
197197
networkManager.Log.ErrorServer(new Context(LogLevel.Developer, $"Failed to deserialize {nameof(NetworkObject)}.").AddInfo(nameof(NetworkObject.GlobalObjectIdHash), serializedObject.Hash).AddInfo(nameof(NetworkObject.NetworkObjectId), serializedObject.NetworkObjectId));
198198
return;
199199
}
200+
201+
// A NetworkObject shown to this client after it migrated into another scene was instantiated in the
202+
// active scene, and the migration was only sent to the clients observing it at the time.
203+
if (networkManager.NetworkConfig.EnableSceneManagement)
204+
{
205+
networkManager.SceneManager.SynchronizeNetworkObjectScene(networkObject);
206+
}
200207
}
201208
else
202209
{

‎com.unity.netcode.gameobjects/Tests/Runtime/NetworkObject/NetworkObjectSceneMigrationObserverTests.cs‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,27 @@ public IEnumerator SceneMigrationWithNoObserversIsNotSent()
106106
LogAssert.NoUnexpectedReceived();
107107
}
108108

109+
/// <summary>
110+
/// An object shown to a client after it migrated while hidden from that client spawns in the authority's scene.
111+
/// </summary>
112+
[UnityTest]
113+
public IEnumerator NetworkShowAfterSceneMigrationSpawnsInAuthorityScene()
114+
{
115+
var authority = GetAuthorityNetworkManager();
116+
var client = GetNonAuthorityNetworkManager();
117+
118+
var networkObject = SpawnObject(m_PrefabNoObserversSpawn, authority).GetComponent<NetworkObject>();
119+
yield return s_DefaultWaitForTick;
120+
Object.DontDestroyOnLoad(networkObject.gameObject);
121+
yield return s_DefaultWaitForTick;
122+
123+
networkObject.NetworkShow(client.LocalClientId);
124+
yield return WaitForSpawnedOn(client, networkObject);
125+
126+
var clientInstance = client.SpawnManager.SpawnedObjects[networkObject.NetworkObjectId];
127+
Assert.AreEqual(client.SceneManager.DontDestroyOnLoadScene, clientInstance.gameObject.scene, $"[Client-{client.LocalClientId}] {networkObject.name} did not spawn in the authority's scene!");
128+
}
129+
109130
/// <summary>
110131
/// A late joining client synchronizes an object that is not in the active scene into the authority's scene.
111132
/// A hybrid prefab instance can spawn after the synchronization completes, once its ghost arrives.

0 commit comments

Comments
 (0)