diff --git a/com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs b/com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs index 00bed26a3c..60e5e9191c 100644 --- a/com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs +++ b/com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs @@ -994,7 +994,7 @@ private void OnSceneLoaded(string sceneName) if (SceneEventData.LoadSceneMode == LoadSceneMode.Single) { // Move all objects to the new scene - MoveObjectsToScene(nextScene); + MoveObjectsFromDontDestroyOnLoadToScene(nextScene); } // The Condition: While a scene is asynchronously loaded in single loading scene mode, if any new NetworkObjects are spawned @@ -1580,15 +1580,15 @@ private void MoveObjectsToDontDestroyOnLoad() foreach (var sobj in objectsToKeep) { - //In case an object has been set as a child of another object it has to be removed from the parent in order to be moved from one scene to another. - if (sobj.gameObject.transform.parent != null) - { - sobj.gameObject.transform.parent = null; - } - if (!sobj.DestroyWithScene || (sobj.IsSceneObject != null && sobj.IsSceneObject.Value && sobj.gameObject.scene == DontDestroyOnLoadScene)) { - UnityEngine.Object.DontDestroyOnLoad(sobj.gameObject); + // Only move objects with no parent as child objects will follow + if (sobj.gameObject.transform.parent == null) + { + UnityEngine.Object.DontDestroyOnLoad(sobj.gameObject); + // Since we are doing a scene transition, disable the GameObject until the next scene is loaded + sobj.gameObject.SetActive(false); + } } else if (m_NetworkManager.IsServer) { @@ -1650,25 +1650,25 @@ private void PopulateScenePlacedObjects(Scene sceneToFilterBy, bool clearScenePl /// Moves all spawned NetworkObjects (from do not destroy on load) to the scene specified /// /// scene to move the NetworkObjects to - private void MoveObjectsToScene(Scene scene) + private void MoveObjectsFromDontDestroyOnLoadToScene(Scene scene) { // Move ALL NetworkObjects to the temp scene var objectsToKeep = m_NetworkManager.SpawnManager.SpawnedObjectsList; foreach (var sobj in objectsToKeep) { - //In case an object has been set as a child of another object it has to be removed from the parent in order to be moved from one scene to another. - if (sobj.gameObject.transform.parent != null) - { - sobj.gameObject.transform.parent = null; - } - if (sobj.gameObject.scene == DontDestroyOnLoadScene && (sobj.IsSceneObject == null || sobj.IsSceneObject.Value)) { continue; } - SceneManager.MoveGameObjectToScene(sobj.gameObject, scene); + // Only move objects with no parent as child objects will follow + if (sobj.gameObject.transform.parent == null) + { + // set it back to active at this point + sobj.gameObject.SetActive(true); + SceneManager.MoveGameObjectToScene(sobj.gameObject, scene); + } } } }