- Home /
Load last scene/level
Hello. I have a simple question. I am making a game and I will thet if the player restart the game, it should be able to load the last scene/level with a button, where he played last time. Is this possible? And how? A script will be good;).
Answer by by0log1c · Jan 05, 2012 at 09:48 PM
Application.LoadLevel(Application.loadedLevelName);
Application.LoadLevel(Application.loadedLevel);
EDIT: My mistake, I didn't realize you want this to work over different game sessions. What you could do is have a script that saves Application.loadedLevel to PlayerPrefs and have the first scene have nothing but a script that'll retrieve the PlayerPrefs and load said level.
Pretty simple, really, here's untested code, to put once on the initial scene, where the player cannot go back.
//LevelLoadManager.js private var currentLevel:int = -1;
function Awake():void { //retrieve and load last level played. DontDestroyOnLoad(gameObject); currentLevel = PlayerPrefs.GetInt("LastLevelLoaded"); Application.LoadLevel(currentLevel); }
function Update():void { //save the current level if it seems to have changed. if(Application.loadedLevel != currentLevel) { currentLevel = Application.loadedLevel; PlayerPrefs.SetInt("LastLevelLoaded",currentLevel); } }
Answer by inkspots · Jan 05, 2012 at 09:59 PM
You need to store a piece of information in some way, You can do this by using PlayerPrefs this wil save a specific piece of information to the disk. and you can load this when ever you want:
for example the player ends in level 10 and the name of the scene is level10 you can use this to save it:
PlayerPrefs.SetString("lastlevel", "level10";
and you can call this info by using:
PlayerPrefs.GetString("lastlevel")
and to put it into context...
var lastlevel = PlayerPrefs.GetString("lastlevel"); Application.LoadLevel (lastlevel);
before the player starts in this level you already need to save this in advance (what if the player would quit the game abruptly, then this data would also be lost) also keep in mind that you need to be more specific when calling information when multiple players would use the application
for more details check... http://unity3d.com/support/documentation/ScriptReference/PlayerPrefs.html
Thank you for the help. I will try this tomorrow and I hope this will work ;) thanks
noone of this scripts are working:( is there anyone with a working script or description?
at the moment I don t have any code. Your code isnt working. $$anonymous$$aybe you have an other one?
Answer by worldofcars · Jan 06, 2012 at 07:02 PM
no one of this scripts are working:( is there anyone with a working script or description?
All these answers people posted should have been plenty of help to get you going.
Just to simply restart the level, use Application.LoadLevel(Application.loadedLevel)
If you're wanting to reload the last played level after the player has quit, and then came back to the game, then you're going to have to go with something like what @BY0LOG1C suggested and playerprefs or some other way of saving and retrieving information.
If none of the codes people posted are working right for you, modify it and make it work how you want it to.
Otherwise try This or something else like it to find someone to program for you ; or use the search bar to try and find other stuff that you can piece together to get it working.
Answer by snowconesolid · Jan 05, 2012 at 09:00 PM
use an if statment
if (Input.GetButtonDown("Jump"))
{ Application.LoadLevel ("The name of your level"); }
The "Jump" button is referring to the Space key if you want you can also use Input.GetKeyDown(KeyCode.whatever key you want pressed))
If you want to load the previous level each time, just make separate scripts. So for example lets say you have 4 levels and you want to go back one level each time the user presses the A key.
so if your in level 4, the script you would have would look like this
if (Input.GetKeyDown(KeyCode.A))
{ Application.LoadLevel (3); }
This will take you back to level 3, now you want to go to level 2, so in your level 3 the script would be this
if (Input.GetKeyDown(KeyCode.A))
{ Application.LoadLevel (2); }
and so on. so Just make the scripts different for each level, be sure that each script is its own separate script and your not just editing the same one over and over again.
You don't understand my question. I want it like this: If the player reach level 10 and close the game and restart it after a few $$anonymous$$utes, than I want this automaticly to load level 10 and not 0 ... do you understand this now?
ahh I see, ok I know what your saying now. Thats a good question. Im not very sure on this one, but you might need to use
Application.LoadLevel(Application.loadedLevel)somehow. but still im not to sure how to get this working
maybe this can help you? http://answers.unity3d.com/questions/198878/restart-current-level.html
Thank you for the answers. I will see this tomorrow;)
Your answer
Follow this Question
Related Questions
Multiple Instances of My Current Scene? 0 Answers
Current scene number 2 Answers
How can I save the player's progress in-game? 1 Answer
How to see what level is running? 2 Answers
What is the Difference betwwen a "Level" and a "scene" 2 Answers