How to make the game character move to the other scene?

Viewed 454

As the title I don't know how to make the game character cross the game_scene_1 to game_scene_2. I make the trigger and I can change to the game_scene_2 but the game character still on the game_scene_1. How to fix it? Please help me.

2 Answers

You could save the player as a prefab and spawn it in with the Start method in the new scene.

Or, if you have a link to the player's gameobject use this:

DontDestroyOnLoad(PLAYER_GO);

If you don't have a link to it you could just put this on the Start method on the player

DontDestroyOnLoad(this.gameObject);

While you can use DontDestroyOnLoad as described by RadiantMin3, you actually can just move an object between scenes! https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.MoveGameObjectToScene.html

Just make sure to load game_scene_2 additively, and then unload game_scene_1 after moving the player. Example:

SceneManager.LoadScene(game_scene_2, LoadSceneMode.Additive);
SceneManager.MoveGameObjectToScene(player.gameObject, game_scene_2);
SceneManager.UnloadScene(game_scene_1);

This will load the game_scene_2 without unloading game_scene_1, move the player while both scenes are loaded and then unload game_scene_1.

Related