How do I load level after each unsuccesful level(player fails to complete the level)

Hi,

I am making a 2d game and going good. There are 8 levels in my game. The player has to complete each level to go to the next. I have a question to ask. If the player fails to complete any level the game will be over and a new scene named as “GameOver” will be displayed. Now in that scene I have two buttons “Restart” and “MainMenu”. I want to restart the scene played just before the GameOver scene if user presses the “Restart” button. I tried with Application.Loadlevel(Application.loadedlevel) but it did not work.

Please help me with this.
Thanks

You’ll have to store what level was played last in some way. Let’s say you’re loading the Game Over scene like this:

void PlayerDied() {
     Application.LoadLevel("GameOver");
}

Then the easiest thing would be to use PlayerPrefs to store the last scene played:

void PlayerDied() {
    PlayerPrefs.SetInt("LastScene", Application.loadedLevel);
    Application.LoadLevel("GameOver");
}

//in the Game Over Scene:

void RestartClicked() {
    int levelToLoad = PlayerPrefs.GetInt("LastScene");
    Application.LoadLevel(levelToLoad);
}

An alternative is to make an object that’s not destroyed when loading level (See DontDestroyOnLoad), and write the last level played to that.