Loading previously played level

How would i go about to load a previously played level? For example i have got a level select screen full of 15 levels. If you die in a level a new scene will be loaded to basically say you have failed with a play again button on. If i pressed that button i want to be able to go back to that level(scene) previously played.

I have an idea where when "Application.LoadLevel();" is called when i select a level, is there a way i can store that level to another variable that can be used later on for example;

selectedLevel = loadedLevel(1);

So when i come to clicking that play again button, i would simply call the selected level.

I hope I have well understood your problem, because I think you could use a pretty simple solution:

1) Create in your first scene an empty object with a script attached.

2) Edit the script to let that object survive when a new level get loaded:

var lastLevelPlayed : int = 0;

function Awake() {

3) Now the object can store global variables in its script, like the last level played and you can access it reading and writing from every other scene you will load.

To access the script and the variable inside it you just have to use the GameObject.Find() function and then the GetComponent() function to get the script itself.

So, supposing you called the empty object "LevelSelector" and the script "LevelScript", you will do:

// Reading
levelSelected = GameObject.Find("LevelSelector").GetComponent("LevelScript").lastLevelPlayed;

// Writing
GameObject.Find("LevelSelector").GetComponent("LevelScript").lastLevelPlayed = 6;

I hope it helps! ^^

[FIRST EDIT (To answer the comment)]

In every scene that represents a level you will need a script to acrivate at level load, like this, I think (supposing you are loading level 3 and using the same names that I proposed before):

function Start() {
GameObject.Find("LevelSelector").GetComponent("LevelScript").lastLevelPlayed = 3;

So you set the current level used, then you play, and you die badly... :)

Now when you call to know which level was played last, in your loader script you just have to put:

levelSelected = GameObject.Find("LevelSelector").GetComponent("LevelScript").lastLevelPlayed;

And you will get 3 in levelSelected value... :)

I hope it is clearer now! ^^


The simplest (but not smart in the long run) way to do it is use Application.LoadedLevel or Application.LoadedLevelName. Application.LoadLevel has 2 overloads, so a line like




or even

Application.LoadLevel(Application.LoadedLevel - 1);
Application.LoadLevel(Application.LoadedLevel + 2);

will work.

A better way is like you said - keep track of the level you're in. To load the level you must have a place that calls Application.LoadLevel with some parameter. Simply save that parameter. If you use the level name - save it in a string variable. If you're using the level number - save the number in an int right after the level was loaded (You can use Application.OnLevelWasLoaded, or just save the variable right when you call the Application.LoadLevel method).

Also - You noted you have a level select screen. This means that you already have some sort of list of levels. You can save the index YOU use to load the scene in a script which has DontDestroyOnLoad in it - that will make the script stay even if you switch scenes, and you can ALWAYS find it and see what level you loaded last.

Why not just use a static variable with the number of the last played level?