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;).


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.

private var currentLevel:int = -1;

function Awake():void
    //retrieve and load last level played.
    currentLevel = PlayerPrefs.GetInt("LastLevelLoaded");

function Update():void
    //save the current level if it seems to have changed.
    if(Application.loadedLevel != currentLevel)
        currentLevel = Application.loadedLevel;

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 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:


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…

no one of this scripts are working:( is there anyone with a working script or description?