- Home /
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.
Answer by FrHaYwOrKs · Apr 21, 2011 at 10:43 AM
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() { DontDestroyOnLoad(transform.gameObject); }
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! ^^
[/FIRST EDIT]
It does help yes, but just a few questions. I have created the gameobject with the script onto level 1 for example and then when i die, and click play again, how would i go about to call the lastLevelPlayed? Now only that it seems that the number of the lastLevelPlayed may not seem to be updating.
I edited the post with some more code samples that could help you better understanding the solution... :)
Hi, Does this solution also work for Android Back $$anonymous$$ey implementation ?
Answer by Cyb3rManiak · Apr 21, 2011 at 10:46 AM
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
Application.LoadLevel(Application.LoadedLevelName);
or
Application.LoadLevel(Application.LoadedLevel);
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.
I kinda get what you mean yes, its just the matter of now doing it :P
Answer by lub0v · Jul 29, 2013 at 03:18 PM
Why not just use a static variable with the number of the last played level?
Your answer
![](https://koobas.hobune.stream/wayback/20220613124216im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Application.Loadlevel("Level Name"); not working on android 2 Answers
Weird behaviour in Application.LoadLevel() 0 Answers
How to create a complicated game over scene ? Help 1 Answer
Teleporting/application.loadlevel 1 Answer
Play Scene help 1 Answer