The question is answered, right answer was accepted
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
Answer by Baste · Oct 13, 2015 at 12:17 PM
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.
No problem!
In the future, please reply to posts by adding a comment, not by adding a new answer. Answers are supposed to be answers, after all!
Follow this Question
Related Questions
How do I Set position when loading a new scene?,How do I set position to an object on load? 0 Answers
Loading new scene in unity 2D 0 Answers
Loading Particular UI from Different Scene 0 Answers
example of new way to load levels 1 Answer
How can I destroy a GameObject after a scene is loaded? 1 Answer