Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by kimardamina · Dec 18, 2014 at 02:10 AM · save scene

Loading the previous game level after closing the game

Hi people, I hope this has not been asked before. I did lots of sleepless night searching for this but I did not get the answer I needed. I also checked the unity documentation on PlayerPrefs but I am not sure if I am doing something wrong.

What i am wishing to do is the following: I am making a game which has about 10 levels, and you discover the levels only after succeeding a previous level. so there is no menu to check which level you at. Now I would like to be able to get back to the last level once I have closed the game/app and reopened it. if I close the game at level 5 I want to be able to get it back at level 5 when I open back the application.

Please, any good person out there who can tell me how to achieve this if it is possible.

Thank you

Comment
Add comment
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

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by MrSoad · Dec 17, 2014 at 10:54 PM

Player Prefs will work for this. As soon as you load a level set(or create and set if one does not exist already) your player pref to your current level number(use an int player pref), make sure you save it or it will be lost when the scene/level ends! Now in your start scene have a check for the existence of the player pref, if it exists get the data(level number) from it and use a "switch"(select) statement in relation to this data to load that level number, if it does not exist load level 1.

Edit : Here are the two code example you asked for :

1) This should be run when you first load up your game, you will need to put the correct scene names into it :

 #pragma strict
 
 private var iLevel_To_Load : int = 0;
 
 function Start() {
 
     if (PlayerPrefs.HasKey("Load_Level_Number")) {
         iLevel_To_Load = PlayerPrefs.GetInt("Load_Level_Number");
     } else {
         Application.LoadLevel ("Your_Level_1_Name");
     }
     
     if (iLevel_To_Load != 0) {
     
         switch (iLevel_To_Load) {
     
             case 1:
                 Application.LoadLevel ("Your_Level_1_Name");
             break;
             
             case 2:
                 Application.LoadLevel ("Your_Level_2_Name");
             break;
             
             case 3:
                 Application.LoadLevel ("Your_Level_3_Name");
             break;
             
             case 4:
                 Application.LoadLevel ("Your_Level_4_Name");
             break;
             
             case 5:
                 Application.LoadLevel ("Your_Level_5_Name");
             break;
         }
     }
 }

2) Place a copy of this script in every scene, make sure you set the correct scene number for each one :

 #pragma strict
 
 function Start() {
 
     //For Level 1, change "1" to whatever the level
     //number is that you have this script on
     PlayerPrefs.SetInt("Load_Level_Number", 1);
     PlayerPrefs.Save();
 }
Comment
Add comment · Show 4 · 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 kimardamina · Dec 18, 2014 at 06:55 AM 0
Share

me Soad, thanks for your reply. Being more of a graphic person, sometimes theory get me lost. Could you possibly write me that javascript code for me to see and understand better?

avatar image InGameDev · Dec 18, 2014 at 10:38 AM 0
Share

when you load any level save that level number to player prefs you can do it by PlayerPrefs.SetInt("loadedLevelNumber", levelValue); and when you start game you can get value by PlayerPrefs.GetInt("loadedLevelNumber", 1);

avatar image kimardamina · Dec 25, 2014 at 07:04 AM 0
Share

This was my Christmas present. It is working as you wrote it. Thank you very much.

avatar image HarshadK · Dec 25, 2014 at 07:06 AM 0
Share

@kimarda$$anonymous$$a You can give @$$anonymous$$rSoad a christmas present by accepting his reply as answer by clicking the tickmark next to this answer (below downvote button) as it is the way to say thank you to the person who provided you with a solution. And it also helps other guys who come visit this question in future to know which is the right answer.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to add resources to an existing android game 1 Answer

Saving Player Position Scene to Scene 0 Answers

Unity opens old version of project. 0 Answers

Optimal way to save minimal serialized scene status, at runtime 1 Answer

Saving your scene and location in game? 1 Answer


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