Use of SceneManager.MoveGameObjectToScene?
Hi,
I am trying to move my player/gameobject to the next scene being loaded.
public void loadLevelasync(int id, GameObject player)
{
StartCoroutine(fadeIn(loadingCanvas, 2.0f));
Scene nextLevel = SceneManager.GetSceneAt(id);
SceneManager.MoveGameObjectToScene(player, nextLevel);
StartCoroutine(loadScene(id));
}
IEnumerator loadScene(int id)
{
AsyncOperation async = SceneManager.LoadSceneAsync(id);
while (!async.isDone)
{
loadingBar.value = async.progress / 0.9f;
yield return null;
}
}
I can load the scene async perfectly, but my gameobject is not in the next loaded scene hierarchy and the lighting is not fully done.
How exactly does MoveGameObjectToScene work?
Thanks, Austin
That didn't work. I debug the code and it seems that Scene$$anonymous$$anager.GetSceneAt(id) is returning the current level. So say if I am on level1 with ID of 0 and level2 has ID of 1. The nextLevel variable has the scene "level1" with ID of 0 after doing Scene$$anonymous$$anager.GetSceneAt(1). So It's not moving the gameobject, but the loadScene coroutine is working and loads level2?
Is this a bug?
public void loadLevelasync(int id, GameObject player)
{
StartCoroutine(fadeIn(loadingCanvas, 2.0f));
StartCoroutine(loadScene(id));
Scene nextLevel = Scene$$anonymous$$anager.GetSceneAt(id);
Debug.Log("Player scene: " + player.scene.name + " Next Scene: " + nextLevel.name);
Scene$$anonymous$$anager.$$anonymous$$oveGameObjectToScene(player, nextLevel);
Debug.Log("Player scene: " + player.scene.name + " Next Scene: " + nextLevel.name);
}
IEnumerator loadScene(int id)
{
AsyncOperation async = Scene$$anonymous$$anager.LoadSceneAsync(id);
while (!async.isDone)
{
loadingBar.value = async.progress / 0.9f;
yield return null;
}
}
Edit: Did some more debugging and I found out that GetSceenAt() is using the indexes that GetAllScenes() uses. The first image in the attachment is the build settings.
public void loadLevelasync(int id, GameObject player)
{
StartCoroutine(fadeIn(loadingCanvas, 2.0f));
StartCoroutine(loadScene(id));
Scene nextLevel = Scene$$anonymous$$anager.GetSceneAt(Scene$$anonymous$$anager.GetActiveScene().buildIndex-1);
Debug.Log("Player scene: " + player.scene.name + " Next Scene: " + nextLevel.name);
Scene$$anonymous$$anager.$$anonymous$$oveGameObjectToScene(player, nextLevel);
Debug.Log("Player scene: " + player.scene.name + " Next Scene: " + nextLevel.name);
Debug.Log("Scene Count: " + Scene$$anonymous$$anager.sceneCount + " : Scene Count in Build Settings: " + Scene$$anonymous$$anager.sceneCountInBuildSettings);
showSceneNames();
}
public void showSceneNames()
{
for(int i = 0; i < Scene$$anonymous$$anager.GetAllScenes().Length; i++)
{
Debug.Log(i + " : " + Scene$$anonymous$$anager.GetAllScenes()[i].name + " " + Scene$$anonymous$$anager.GetAllScenes()[i].buildIndex);
}
}
The second image is the debug output after the next level is loaded.
As you can see from the debug output, i cannot use the indexes used for the build settings. I still cannot move the gameobject though. The gameobject is not in the next scene when the next level is loaded [1]: /storage/temp/65436-drrso.jpg [2]: /storage/temp/65437-khqme.jpg
Answer by Zackton · Aug 16, 2016 at 06:47 AM
@sk8terboy4 Instead of using SceneManager.MoveGameObjectToScene(player, nextLevel)
, you can simply use:
Object.DontDestroyOnLoad(player.gameObject); //You might be able to just use 'player'.
Or if the script you are on is the player's script, you can simply use:
Object.DontDestroyOnLoad(this.gameObject); //You might be able to just use 'this'
Sorry about the long time waiting, I have only come across this problem today.
Answer by RomanLed · Oct 07, 2016 at 09:24 PM
This is how to keep objects (different than moving objects from scene to scene however it might help you)... https://www.youtube.com/watch?v=S9h_iu4Zx5I&feature=youtu.be