Skip to content

Commit f996f9a

Browse files
committed
fix: Spawning objects during scene switch
1 parent 824faaa commit f996f9a

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

MLAPI/SceneManagement/NetworkSceneManager.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public static class NetworkSceneManager
3737
private static bool isSwitching = false;
3838
internal static uint currentSceneIndex = 0;
3939
internal static Guid currentSceneSwitchProgressGuid = new Guid();
40+
internal static bool isSpawnedObjectsPendingInDontDestroyOnLoad = false;
4041

4142
internal static void SetCurrentSceneIndex()
4243
{
@@ -113,6 +114,8 @@ internal static void OnSceneSwitch(uint sceneIndex, Guid switchSceneGuid, Stream
113114

114115
// Move ALL networked objects to the temp scene
115116
MoveObjectsToDontDestroyOnLoad();
117+
118+
isSpawnedObjectsPendingInDontDestroyOnLoad = true;
116119

117120
string sceneName = sceneIndexToString[sceneIndex];
118121

@@ -164,6 +167,8 @@ private static void OnSceneLoaded(Guid switchSceneGuid, Stream objectStream)
164167

165168
// Move all objects to the new scene
166169
MoveObjectsToScene(nextScene);
170+
171+
isSpawnedObjectsPendingInDontDestroyOnLoad = false;
167172

168173
if (NetworkingManager.Singleton.IsServer)
169174
{

MLAPI/Spawning/SpawnManager.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using MLAPI.Internal;
88
using MLAPI.Logging;
99
using MLAPI.Messaging;
10+
using MLAPI.SceneManagement;
1011
using MLAPI.Security;
1112
using MLAPI.Serialization;
1213
using MLAPI.Serialization.Pooled;
@@ -240,12 +241,27 @@ internal static NetworkedObject CreateLocalNetworkedObject(bool softCreate, ulon
240241
// Create the object
241242
if (customSpawnHandlers.ContainsKey(prefabHash))
242243
{
243-
return customSpawnHandlers[prefabHash](position.GetValueOrDefault(Vector3.zero), rotation.GetValueOrDefault(Quaternion.identity));
244+
NetworkedObject networkedObject = customSpawnHandlers[prefabHash](position.GetValueOrDefault(Vector3.zero), rotation.GetValueOrDefault(Quaternion.identity));
245+
246+
if (NetworkSceneManager.isSpawnedObjectsPendingInDontDestroyOnLoad)
247+
{
248+
GameObject.DontDestroyOnLoad(networkedObject.gameObject);
249+
}
250+
251+
return networkedObject;
244252
}
245253
else
246254
{
247255
GameObject prefab = NetworkingManager.Singleton.NetworkConfig.NetworkedPrefabs[GetNetworkedPrefabIndexOfHash(prefabHash)].Prefab;
248-
return ((position == null && rotation == null) ? MonoBehaviour.Instantiate(prefab) : MonoBehaviour.Instantiate(prefab, position.GetValueOrDefault(Vector3.zero), rotation.GetValueOrDefault(Quaternion.identity))).GetComponent<NetworkedObject>();
256+
257+
NetworkedObject networkedObject = ((position == null && rotation == null) ? MonoBehaviour.Instantiate(prefab) : MonoBehaviour.Instantiate(prefab, position.GetValueOrDefault(Vector3.zero), rotation.GetValueOrDefault(Quaternion.identity))).GetComponent<NetworkedObject>();
258+
259+
if (NetworkSceneManager.isSpawnedObjectsPendingInDontDestroyOnLoad)
260+
{
261+
GameObject.DontDestroyOnLoad(networkedObject.gameObject);
262+
}
263+
264+
return networkedObject;
249265
}
250266
}
251267
else

0 commit comments

Comments
 (0)