- Home /
How to remember the level/scene?
I am very new to Unity and coding and i'm building a platformer game. So i'm looking for a script that would remember the current scene when leaving the game (or that would autosave the current scene), and when re-opening the game it should start the scene that it saved. I've tried a few scripts with PlayerPrefs but somehow they don't seem to work. Also when i'm testing my game and these scripts it always starts with the scene that is selected, and not with the most recent scene. Does anyone know a solution for this? Or am i just overlooking something that's very obvious?
Thanks in advance.
Answer by DiegoSLTS · Feb 22, 2015 at 04:26 PM
I'm not sure what you mean with "it always starts with the scene that is selected, and not with the most recent scene".
When you press play in the editor it always plays the scene you're editing.
When you run a build it always start from the first scene in the "Build Settings..." dialog.
If you want your game to start in a different scene each time you can't do exactly that, but you can create a scene that displays nothing and loads a different scene according to some saved value.
Create a new scene, remove the Main Camera component and add an Empty Object. Create a new script for that object and on it's Start function do something like this:
void Start() {
string levelName = PlayerPrefs.GetString("lastLevel"); //this assumes you save a string in PlayerPrefs at some point that's the name of the scene with that level
if (levelName == null)
levelName = "Some scene"; //the default scene that should be loaded when you play for the first time
Application.LoadLevel(levelName);
}
Your answer
Follow this Question
Related Questions
Argument out of range 1 Answer
PlayerPrefs Problem crashing with SetBool 2 Answers
How to use PlayerPrefs? 2 Answers
Save level 3 Answers
How to save resources configuration file for unity3d? 0 Answers