• 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 EmeraldWanderer · Sep 03, 2017 at 09:22 PM · levelselectionlevel select

locking levels based on new game

Hey everyone! I have been working on this game for a while, and I have a script set up for level selection. So far, it works great, but I am running into one snag. I play the game and make it through a few levels. However, if I try to hit New Game, all of the levels I have played stay unlocked. Basically, the locks won't reset and I don't know how to fix it. Anyone have any ideas of what I can do to get this fixed?

 public class LevelSelectManager : MonoBehaviour {
 
     public string[] levelTags;
     public GameObject[] locks;
     public bool[] levelUnlocked;
         
     public string[] levelName;
     
     private bool isPressed;
 
     public Button[] button;
         
     void Start()
     {
         GetComponent<Button>();
 
         for(int i = 0; i < levelTags.Length; i++)
         {
             if(PlayerPrefs.GetInt(levelTags[i]) == null)
             {
                 levelUnlocked[i] = false;
                 button[i].interactable = false;
             }
             else if (PlayerPrefs.GetInt(levelTags[i]) == 0)
             {
                 levelUnlocked[i] = false;
                 button[i].interactable = false;
             }
             else
             {
                 levelUnlocked[i] = true;
                 button[i].interactable = true;
             }
 
             if (levelUnlocked[i])
             {
                 button[i].interactable = true;
                 locks[i].SetActive(false);
             }
 
         }
                
 
     }
     
     public void LoadScene(string levelName)
     {
         Application.LoadLevel(levelName);            
     }
 }

And yes, if you are a newbie who is trying to figure this stuff out, you are more than welcome to use this script!

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
0

Answer by jasonlu00 · Sep 03, 2017 at 11:35 PM

When you unlock your level, you need to save the changes to PlayerPref as well:

 private void UnlockLevel(int index){
   levelUnlocked[index] = true;
   button[index].interactable = true;
   locks[index].SetActive(false);
   PlayerPrefs.SetInt(levelTags[index], 1);
 }
Comment
Add comment · Show 2 · 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 EmeraldWanderer · Sep 04, 2017 at 02:21 AM 0
Share

Hey thanks for your quick reply! I am going to try and find the PlayerPref, but I am actually unable to find that script. I looked in H$$anonymous$$CU\Software[company name][product name], but it wasn't in there anywhere. Any idea where else I could find it?

avatar image haruna9x EmeraldWanderer · Sep 04, 2017 at 03:09 AM 0
Share

PlayerPrefabs is only saved to disk when you quit the game. You may have to use PlayerPrefabs.Save().

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.

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

70 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

Related Questions

Level selection menu (grid) and scrolling it!! 1 Answer

Level Selection. Any Good Direction To Start? 1 Answer

Level selection from main menu? 4 Answers

simple android swipe controls for level selection? 1 Answer

Make a Button For Every String in an Array of Level Names 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