• 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 JackReivaj · Apr 08 at 08:54 AM · menulevelslevel loadunlocklevel select

Problems with unlocking levels

In the menu I put an option for the levels to be unlocked as I pass them. In this case, once passed, I can choose the level that I already passed. At the moment I have 10 levels. To give an example, it happens to me that when I choose to start from level 5 and go to level 6, the following are blocked. From 7 to 10 they are blocked and I have to unblock them again. How could I modify the code so that once it is unlocked it does not lock again? Here is the script i'm using:

  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  using UnityEngine.UI;
  using UnityEngine.SceneManagement;
   
  public class LevelSelector : MonoBehaviour
  {  
      public Button[] levelBTN;  
      public SceneLogic sceneLogic;
   
      void Start()
      {    
          //To unlock levels
          int level = PlayerPrefs.GetInt("level", 1);
   
          for (int i = 0; i < levelBTN.Length; i++)
          {
              if (i + 1 > level)
                  levelBTN[i].interactable = false;        
          }
      }
 
      public void levelToLoad(int level)
      {
          SceneManager.LoadScene(level);
       
      }      
   
      public void ButtonPressed1()
      {    
          sceneLogic.IndiceDeNiveles = 0;
          PlayerPrefs.SetInt("CorrespondingLevel", sceneLogic.LevelsIndex);
      }  
      public void ButtonPressed2()
      {    
          sceneLogic.IndiceDeNiveles = 1;
          PlayerPrefs.SetInt("CorrespondingLevel", sceneLogic.LevelsIndex);
      }  
      public void ButtonPressed3()
      {
          sceneLogic.IndiceDeNiveles = 2;
          PlayerPrefs.SetInt("CorrespondingLevel", sceneLogic.LevelsIndex);
      }  
      public void ButtonPressed4()
      {
          sceneLogic.IndiceDeNiveles = 3;
          PlayerPrefs.SetInt("CorrespondingLevel", sceneLogic.LevelsIndex);
      }  
      public void ButtonPressed5()
      {
          sceneLogic.IndiceDeNiveles = 4;
          PlayerPrefs.SetInt("CorrespondingLevel", sceneLogic.LevelsIndex);
      }  
      public void ButtonPressed6()
      {
          sceneLogic.IndiceDeNiveles = 5;
          PlayerPrefs.SetInt("CorrespondingLevel", sceneLogic.LevelsIndex);
      }  
      public void ButtonPressed7()
      {
          sceneLogic.IndiceDeNiveles = 6;
          PlayerPrefs.SetInt("CorrespondingLevel", sceneLogic.LevelsIndex);
      }  
      public void ButtonPressed8()
      {
          sceneLogic.IndiceDeNiveles = 7;
          PlayerPrefs.SetInt("CorrespondingLevel", sceneLogic.LevelsIndex);
      }  
      public void ButtonPressed9()
      {
          sceneLogic.IndiceDeNiveles = 8;
          PlayerPrefs.SetInt("CorrespondingLevel", sceneLogic.LevelsIndex);
      }  
      public void ButtonPressed10()
      {
          sceneLogic.IndiceDeNiveles = 9;
          PlayerPrefs.SetInt("CorrespondingLevel", sceneLogic.LevelsIndex);
      } 
  }

I should create a function that if it is 10 levels and I choose level 5, it should check if levels 6 to 10 are locked or unlocked. If they are unlocked, I should leave them unlocked if I go to 6. And if they are locked, I should leave them locked. Any idea how to do that?

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 Darkluca · Apr 08 at 08:01 AM

You are setting the Playerprefs int but not getting it again.

Code something like:

 if(PlayerPrefs.GetInt("CorrespondingLevel") == *wanted level*)
 {
 //unlock code
 }
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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

116 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

Related Questions

How to make an exit level trigger? 3 Answers

Purchase Level Unlock System 1 Answer

How to make PlayerPrefs delete if the player didn't touch game finish collider 1 Answer

I'm trying to add an options button to my game but it's not responding 0 Answers

How to manage multiple levels in the 2D game ? 3 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