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 ethanol3310 · Sep 28, 2020 at 10:53 AM · playerprefsscene-loadingunlock

PlayerPrefs and Unlocking Scenes

Hi, I have just set up a player prefs variable that whenever a scene is loaded, sets the value of the int to the build index. Here is the script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine.SceneManagement;
 using UnityEngine;
 
 public class CheckLastScene : MonoBehaviour
 {
     // Start is called before the first frame update
     void Start()
     {
 
     }
 
     // Update is called once per frame
     void Update()
     {
         Debug.Log(PlayerPrefs.GetInt("loadedLevel"));
         PlayerPrefs.SetInt("loadedLevel", SceneManager.GetActiveScene().buildIndex);
     }
 }

But the problem is that the script that unlocks the buttons once the level is completed and you go to the level selection resets once I revisit or proceed to the next level. Here is the script(Sorry it's long):

 using System.Collections;
 using System.Collections.Generic;
 
 using UnityEngine;
 using UnityEngine.SceneManagement;
 using UnityEngine.UI;
 
 public class UnlockAll : MonoBehaviour
 {
     public Button[] buttons;
     public InputField input;
     public Text text;   
     public string answer;
     public ChangeScenes script;
     public InputField winText;
     public Button b1;
     public Button b2;
     public Button b3;
     public Button b4;
     public Button b5;
     public Button b6;
     public Button b7;
     public Button b8;
     public Button b9;
     public Button b10;
     public Button b11;
     public Button b12;
     public Button b13;
     public Button b14;
     public Button b15;
     public string win = "Demo Reached, More Levels Coming Soon!";
 
     void Start()
     {
         Debug.Log(PlayerPrefs.GetInt("loadedLevel"));
     }
 
     void Update()
     {
         if (PlayerPrefs.GetInt("loadedLevel") >= 22) 
         {
             b1.enabled = true;
             b2.enabled = true;
             b3.enabled = true;
             b4.enabled = true;
             b5.enabled = true;
             b6.enabled = true;
             b7.enabled = true;
             b8.enabled = true;
             b9.enabled = true;
             b10.enabled = true;
             b11.enabled = true;
             b12.enabled = true;
             b13.enabled = true;
             b14.enabled = true;
             b15.enabled = true;
         }
         answer = input.text;
 
         if (answer == "UnlockAll")
         {
             b1.enabled = true;
             b2.enabled = true;
             b3.enabled = true;
             b4.enabled = true;
             b5.enabled = true;
             b6.enabled = true;
             b7.enabled = true;
             b8.enabled = true;
             b9.enabled = true;
             b10.enabled = true;
             b11.enabled = true;
             b12.enabled = true;
             b13.enabled = true;
             b14.enabled = true;
             b15.enabled = true;
 
             Debug.Log("Levels Enabled");
         }
 
         if (PlayerPrefs.GetInt("loadedLevel") == 5) 
         {
             Debug.Log("Level 2 Unlocked");
             b1.interactable = true;
             b2.interactable = true;
         }
 
         if (PlayerPrefs.GetInt("loadedLevel") == 6)
         {
             Debug.Log("Level 3 Unlocked");
             b1.interactable = true;
             b2.interactable = true;
             b3.interactable = true;
         }
 
         if (PlayerPrefs.GetInt("loadedLevel") == 7)
         {
             Debug.Log("Level 4 Unlocked");
             b1.interactable = true;
             b2.interactable = true;
             b3.interactable = true;
             b4.interactable = true;
         }
 
         if (PlayerPrefs.GetInt("loadedLevel") == 8)
         {
             Debug.Log("Level 5 Unlocked");
             b2.interactable = true;
             b3.interactable = true;
             b4.interactable = true;
             b5.interactable = true;
         }
 
         if (PlayerPrefs.GetInt("loadedLevel") == 9)
         {
             Debug.Log("Level 6 Unlocked");
             b2.interactable = true;
             b3.interactable = true;
             b4.interactable = true;
             b5.interactable = true;
             b6.interactable = true;
         }
 
         if (PlayerPrefs.GetInt("loadedLevel") == 10)
         {
             Debug.Log("Level 7 Unlocked");
             b2.interactable = true;
             b3.interactable = true;
             b4.interactable = true;
             b5.interactable = true;
             b6.interactable = true;
             b7.interactable = true;
         }
 
         if (PlayerPrefs.GetInt("loadedLevel") == 11)
         {
             Debug.Log("Level 8 Unlocked");
             b2.interactable = true;
             b3.interactable = true;
             b4.interactable = true;
             b5.interactable = true;
             b6.interactable = true;
             b7.interactable = true;
             b8.interactable = true;
         }
 
         if (PlayerPrefs.GetInt("loadedLevel") == 12)
         {
             Debug.Log("Level 9 Unlocked");
             b2.interactable = true;
             b3.interactable = true;
             b4.interactable = true;
             b5.interactable = true;
             b6.interactable = true;
             b7.interactable = true;
             b8.interactable = true;
             b9.interactable = true;
         }
 
         if (PlayerPrefs.GetInt("loadedLevel") == 13)
         {
             Debug.Log("Level 10 Unlocked");
             b2.interactable = true;
             b3.interactable = true;
             b4.interactable = true;
             b5.interactable = true;
             b6.interactable = true;
             b7.interactable = true;
             b8.interactable = true;
             b9.interactable = true;
             b10.interactable = true;
         }
 
         if (PlayerPrefs.GetInt("loadedLevel") == 14)
         {
             Debug.Log("Level 11 Unlocked");
             b2.interactable = true;
             b3.interactable = true;
             b4.interactable = true;
             b5.interactable = true;
             b6.interactable = true;
             b7.interactable = true;
             b8.interactable = true;
             b9.interactable = true;
             b10.interactable = true;
             b11.interactable = true;
         }
     }
 }

Thanks in advance for anyone who helps as I am quite frustrated

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

Answer by Namey5 · Sep 28, 2020 at 10:08 PM

If you don't want progress to regress, then you should check that the new value isn't lower than the value that is already saved. That aside, there are a few things to point out here;

First off, I can't see any reason to have this stuff in Update - Update is called every frame, but all of this only needs to happen once at any one time. You also access the same variable from playerprefs many times; it would be worth caching that in a local variable and reusing that instead.

One other thing I would do is use an array of buttons rather than declare each button individually; you are already naming them with an index, so changing to an array wouldn't make much of a difference, however it would allow you to do things much more efficiently.

Here's how those changes would look;

  public class CheckLastScene : MonoBehaviour
  {
      void Start()
      {
          int lastLoadedLevel = PlayerPrefs.GetInt ("loadedLevel");
          int currentLevel = SceneManager.GetActiveScene().buildIndex;

          Debug.Log(lastLoadedLevel);
          //Make sure we are only making new progress
          if (currentLevel > lastLoadedLevel)
              PlayerPrefs.SetInt("loadedLevel", currentLevel);
      }
  }

And in the main class;

  public class UnlockAll : MonoBehaviour
  {
      public Button[] buttons;
      public InputField input;
      public Text text;   
      public string answer;
      public ChangeScenes script;
      public InputField winText;
      //'b1' = 'levelButtons[0]', 'b2' = 'levelButtons[1]', etc.
      public Button[] levelButtons = new Button[15];
      public string win = "Demo Reached, More Levels Coming Soon!";
  
      void Start()
      {
          Debug.Log(PlayerPrefs.GetInt("loadedLevel"));
          //Call this once when the level is loaded
          UnlockLevels ();
      }
  
      void UnlockLevels()
      {
          //Store these for reuse
          int level = PlayerPrefs.GetInt("loadedLevel");
          answer = input.text;
          if (level >= 22 || answer == "UnlockAll") 
          {
              //Iterate through every button and enable them all
              for (int i = 0; i < levelButtons.Length; i++)
                  levelButtons[i].interactable = true;
  
              Debug.Log("Levels Enabled");
          }
          else if (level > 4 && level < 15)    //If we are within the right range
          {
              //Iterate through every button up to the required one and enable them
              for (int i = 0; i < level - 3; i++)
                  levelButton[i].interactable = true;
 
              Debug.LogFormat ("Level {0} Unlocked", level - 2);
          }
      }
  }
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

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

145 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 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 to make PlayerPrefs delete if the player didn't touch game finish collider 1 Answer

How to create a main menu with singleton pattern? 2 Answers

Move a gameobject after loading new scene? 2 Answers

Help with loading player position after scene change 3 Answers

The player goes through a door and change scene. If I come back, how can I have the player appear where it left off? 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