I want to create a menu for my game that records the position of the player and makes the actual level position the player where it last was in the menu. How can I achieve that?
Here are some options that you can research for saving variables (esp. small values):
- PlayerPrefs
- static variable
- a script on a game object using DontDestroyOnLoad
For one variable in your situation, I would say that option 1 or 2 is probably the best. If you want it to be saved during play sessions, as well, then I would say option #1 is your best choice.
Thank you!
You’re welcome.
For something that you want to both persist between scenes and between game sessions (which is what it sounds like), I generally use a combination of #1 and #3 from methos5k’s list.
So in the first scene the game starts in I instantiate a gameobject, if this gameobject does not already exist, that stores this information. A script on this gameobject sets DontDestroyOnLoad and then checks playerprefs if the information is stored there, and if not then sets a default value. Any changes to this information by any scene get updated to this gameobject. Either when the information changes, or when the player clicks an exit game button, the script writes it out to playerprefs.
An example of what I commonly use this for is options such as a music volume setting, but you could just as easily use the same technique for a player’s level progress.