• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
This question was closed Oct 18, 2015 at 01:26 PM by Baste for the following reason:

The question is answered, right answer was accepted

avatar image
Question by sovan · Oct 13, 2015 at 11:54 AM · loadlevelload sceneload level

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

Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
Best Answer

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.

Comment

People who like this

0 Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sovan · Oct 18, 2015 at 11:57 AM 0
Share

Thanks @Baste it worked.

avatar image Baste sovan · Oct 18, 2015 at 01:26 PM 0
Share

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!

avatar image sovan Baste · Oct 18, 2015 at 01:52 PM 0
Share

sure. Thanks again.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Problem: I am in multi display and after a load the device 2 and 3 does not work.,Problem: I am in multi display and after a load the device 2 and 3 does not work 0 Answers

Destroy object when a new scene loads 0 Answers

How to fix Loading Screen? 0 Answers

SceneManager.LoadScene problem 0 Answers

Hello everyone , is there a way to create a box which prompts for password on the first screen and when the password is entered user enters the app ?how can i do it ? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges