Does SceneManager.MoveGameObjectToScene work?
Hello.
Im trying to fill a gameObject with information in one scene, and then move it into another scene.
using UnityEngine; using UnityEngine.SceneManagement; using System.Collections;
public class EncounterPackage : MonoBehaviour {
void ChangeScene(GameObject encounter)
{
Scene battleScene = SceneManager.GetSceneAt(SceneManager.sceneCount - 1);
GameObject package = Instantiate(gameObject) as GameObject;
SceneManager.MoveGameObjectToScene(package, battleScene);
SceneManager.LoadScene(1);
}
}
It loads the new scene, but the package object is not in it. Am i using MoveObjectToScene correctly here?
Thanks in advance, Vetle
You can't move anything to a scene that you haven't loaded yet. If you could, that would mean the package would have to be serialized to the scene data on disk so it is there when the scene loads. This would mean the next time you run the game, the packages from all previous play sessions would be there too.
Look at @saud_ahmed020 s answer.
Answer by AntFitch · Mar 30, 2017 at 07:07 PM
Building on the answer by @RomanLed. You can use this script to move an object from one scene to another:
using UnityEngine;
using UnityEngine.SceneManagement;
static public class MoveObjectToNewScene
{
static GameObject targetObject;
static string targetSceneName;
static Scene currentScene;
static Scene newScene;
/// <summary>
/// Move a GameObject from the current scene to another scene.
/// </summary>
/// <param name="sceneName">Name of the scene you want to load.</param>
/// <param name="targetGameObject">GameObject you want to move to the new scene.</param>
static public void LoadScene(string sceneName, GameObject targetGameObject)
{
// set some globals
targetObject = targetGameObject;
targetSceneName = sceneName;
// get the current active scene
currentScene = SceneManager.GetActiveScene();
// load the new scene in the background
SceneManager.LoadSceneAsync(targetSceneName, LoadSceneMode.Additive);
// Attach the SceneLoaded method to the sceneLoaded delegate.
// SceneLoaded will be called when the new scene is loaded.
SceneManager.sceneLoaded += SceneLoaded;
}
/// <summary>
/// After new scene loads, move GameObject from current scene to new scene.
/// When finished, unload current scene. The new scene becomes current scene.
/// </summary>
/// <param name="newScene">New scene that was loaded.</param>
/// <param name="loadMode">Mode that was used to load the scene.</param>
static public void SceneLoaded(Scene newScene, LoadSceneMode loadMode)
{
// remove this method from the sceneLoaded delegate
SceneManager.sceneLoaded -= SceneLoaded;
// get the scene we just loaded into the background
newScene = SceneManager.GetSceneByName(targetSceneName);
// move the gameobject from scene A to scene B
SceneManager.MoveGameObjectToScene(targetObject.gameObject, newScene);
// unload scene A
SceneManager.UnloadSceneAsync(currentScene);
}
}
Here is how you could use this script:
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour {
void Start () {
MoveObjectToNewScene.LoadScene("sceneA", this.gameObject);
}
}
Answer by saud_ahmed020 · Jun 09, 2016 at 11:19 AM
@The Vaffel When you load the scene the previous scene has destroyed.
Use DontDestroyOnLoad method. e.g
DontDestroyOnLoad(gameobject);
SceneManager.LoadScene(1);
Answer by Kuncu · Jul 12, 2016 at 10:54 PM
But MoveGameObjectToScene method is used before the LoadScene Method.
Post comments as comments and answers as answers please.
Answer by JonasD2K · Sep 16, 2016 at 01:13 PM
@The Vaffel I've been looking around trying to find solutions to avoid using DontDestroyOnLoad (since it's highly recommended not to use it in the documentation), but I'm having the same problem with moving objects from one scene to another. Have you found a solution ?
Post comments as comments and answers as answers please.
@JonasD2$$anonymous$$ i show how is recommended in the documentation to keep objects in this video... https://www.youtube.com/watch?v=i2W5O5qxCuo
Answer by RomanLed · Oct 08, 2016 at 08:17 PM
@The Vaffel This script, when attached to any gameObject, will load a scene and move the gameObject to that scene. When calling the "ChangeScene" method, send it the scene index of the next scene.
void ChangeScene(int nextSceneIndex) //index of scene to move to
{
SceneManager.LoadScene(nextSceneIndex, LoadSceneMode.Additive);
Scene nextScene = SceneManager.GetSceneAt(SceneManager.sceneCount - 1);
SceneManager.MoveGameObjectToScene(gameObject, nextScene);
}
the above method will load another scene additively and keep the first, while moving the object. the coroutine below will delete the previous scene. make sure to yield null.
IEnumerator ChangeScene()
{
SceneManager.LoadScene(nextSceneIndex, LoadSceneMode.Additive);
Scene nextScene = SceneManager.GetSceneAt(SceneManager.sceneCount - 1);
SceneManager.MoveGameObjectToScene(gameObject, nextScene);
yield return null;
SceneManager.UnloadScene(nextSceneIndex - 1);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612082552im_/https://answers.unity.com/themes/thub/images/avi.jpg)