Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 ianelirod2005 · Sep 04, 2021 at 08:48 PM · levelselectionlostprogress

Need help with level unlock system.

I have a level unlock system for 10 levels so far. And it works well at first, but if you go to a level you have passed after playing for a while, you lose your progress. Example: if you passed level 8 and unlocked level 9 you can play level 9. But if you go to menu, and select level 3, and pass level 3, you can now only play levels 1-4. And levels 5-9 which were previously unlocked are now locked again. (Locked = Disabled) This is my level selector code:

 using UnityEngine;
 using UnityEngine.SceneManagement;
 using UnityEngine.UI;
 
 public class LEVELSELECTOR : MonoBehaviour
 {
     public class LevelSaver : MonoBehaviour
     {    
          //This function should be called through the button's OnClick
          public void SaveLevel(int id){
              PlayerPrefs.SetInt("levelReached", id);
          }
     }
 
    public EndTrigger endTrigger;    
    public GameManager gameManager;    
    public Button[] levelButtons;
     
     void Start ()        
     {
         int levelReached = PlayerPrefs.GetInt ("levelReached", 1);
     
         for (int i = 0; i < levelButtons.Length; i++)
         {
             if (i + 1 > levelReached)
         levelButtons[i].interactable = false;   

             if (PlayerPrefs.GetInt("levelReached") < gameManager.LevelToUnlock)
             {
                 PlayerPrefs.SetInt("levelReached", gameManager.LevelToUnlock);   
             }
         }
     }

     public void Level01()
     {
         SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
     }
 
     public void Level02()
     {
         SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 2);
     }
 
     public void Level03()
     {
         SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 3);
     }
 
     public void Level04()
     {
         SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 4);
     }
 
     public void Level05()
     {
         SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 5);
     }
 
     public void Level06()
     {
         SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 6);
     }
 
     public void Level07()
     {
         SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 7);
     }
 
     public void Level08()
     {
         SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 8);
     }
 
     public void Level09()
     {
         SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 9);
     }
 
     public void Level10()
     {
         SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 10);
     }
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by rh_galaxy · Sep 05, 2021 at 01:02 PM

Where do you set gameManager.LevelToUnlock?

 //doing this in a for loop may give the wrong behavior? I think you should remove it.
 //if (PlayerPrefs.GetInt("levelReached") < gameManager.LevelToUnlock)
 //{
 //   PlayerPrefs.SetInt("levelReached", gameManager.LevelToUnlock);   
 //}

You need to do PlayerPrefs.SetInt("levelReached", ...) only at the time you reach a level above the present, and nowhere else.

It is not obvious that SaveLevel() will run when you click on a button... move it to that place. Every time you do a Set, you should also run this to make it stick after the program is exited.

 PlayerPrefs.Save();
Comment
Add comment · 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
0

Answer by ianelirod2005 · Sep 06, 2021 at 03:15 AM

@rh_galaxy PlayerPrefs.SetInt("levelReached", ...) does go when you reach a level above the present. That works fine, the problem is when you go back to an earlier level, levels you had aleady reached become disabled. This is my game manager script. Reference to game manager is supoosedly going to this script.

using UnityEngine; using UnityEngine.SceneManagement;

public class GameManager : MonoBehaviour {

 public int End = 1;
 
 public string nextLevel = "Level02";
 
 public int LevelToUnlock = 2;
 
 public EndTrigger endTrigger;
 
 bool gameHasEnded = false;
 
 public float restartDelay = 1f;
 
 public GameObject completeLevelUI;

 public void CompleteLevel ()
 
 
 
 {
     completeLevelUI.SetActive(true);
     PlayerPrefs.SetInt("levelReached", LevelToUnlock);
     if (PlayerPrefs.GetInt("levelReached") < LevelToUnlock)
         {
             PlayerPrefs.SetInt("levelReached", LevelToUnlock);   
           } 
     if (LevelToUnlock > PlayerPrefs.GetInt("levelReached", 1))

{ PlayerPrefs.SetInt("levelReached", LevelToUnlock); }

 }
 
 
 public void EndGame ()
 {
  if (gameHasEnded == false)
  {
  gameHasEnded = true;
  Debug.Log("Game Over");
  Invoke("Restart", restartDelay);
  
  }

}

 void Restart ()
 {
     SceneManager.LoadScene(SceneManager.GetActiveScene().name);
 }
 
 
 

 

}

Comment
Add comment · Show 1 · 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 rh_galaxy · Sep 06, 2021 at 09:00 AM 0
Share

If you follow my tip and remove all but one place where you have PlayerPrefs.SetInt("levelReached", ...) it will become easier, now you have it in five places and you have trouble.

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

127 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I save the player's progress in-game? 1 Answer

Level selection from main menu? 4 Answers

Level Selection. Any Good Direction To Start? 1 Answer

Make a Button For Every String in an Array of Level Names 1 Answer

simple android swipe controls for level selection? 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