All my information about player , guns are stored in monobehaviour scripts which are placed on them.When I press that button , scene2 gets activated.However , what should I do to "teleport" my player in the next scene?I would also like to get there my UI(healthbar , bullets).I've watched some videos about singletons but I'm not sure if that's my case.Any advice?
Asked
Active
Viewed 65 times
-1
-
.. either use [`DontDestroyOnLoad`](https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html) or use [additive scene loading](https://docs.unity3d.com/ScriptReference/SceneManagement.LoadSceneMode.Additive.html) ... also see [How to pass data between Scenes](https://stackoverflow.com/questions/32306704/how-to-pass-data-between-scenes-in-unity) – derHugo May 17 '21 at 18:45
1 Answers
1
You should use DontDestroyOnLoad()
. This does what it says. Basically, it doesn’t destroy your game object when you load the new scene. You should remove the player prefab from the other scene, and put this in your player script:
...
void Awake()
{
DontDestroyOnLoad(gameObject);
...
}
...
This way, if all of your variables are stored on this script, they will save because they are not being destroyed through scenes.
This will save game objects childed to this. If it doesn’t, make other variables for the children. Then, add multiple DontDestroyOnLoad(); statements with the different variables in the different statements.
(Note: this will not save it if you stop the game or close the application. It will keep the values through scenes, not game ends or app closes.)

gbe
- 1,003
- 1
- 7
- 23