Search Unity

How to make lock/unlock level system with playerprefs(UI Button)?

Discussion in 'Android' started by domy01, Oct 30, 2016.

  1. domy01

    domy01

    Joined:
    Oct 12, 2016
    Posts:
    17
    Hi everyone, In my 2D platforms I'm trying to make an unlock/lock level system with playerprefs...
    Unfortunately I do not know how to use playerprefs ... Can you help me?
    I have a script for change scene.. i need only a script that allows me to change a button (level lock)
    with another button (level unlock) and save progress... Thanks

    (Sorry for my english)
     
  2. rawegames

    rawegames

    Joined:
    Aug 1, 2016
    Posts:
    91
    Attach this script somewhere in your scene, then set it's "SaveLevel" function to the level unlock Button's OnClick, or call the function via code:
    Code (csharp):
    1.  
    2. public class LevelSaver : MonoBehaviour{
    3.   //This function should be called through the button's OnClick
    4.   public void SaveLevel(int id){
    5.     PlayerPrefs.SetInt("level",id);
    6.   }
    7. }
    8.  
    Attach this script to the level lock Button, then, at the inspector, set the "levelRequirement" field to the level that the Button should be unlocked at.
    Code (csharp):
    1.  
    2. public class LevelLock : MonoBehaviour{
    3.   //This field should be set by the inspector
    4.   [SerializeField] int levelRequirement;
    5.   public void Start(){
    6.     int currentLevel = PlayerPrefs.GetInt("level");
    7.     bool levelUnlocked = currentLevel >= levelRequirement;
    8.     GetComponent<Button>().interactable = levelUnlocked;
    9.   }
    10. }
    11.  
    I think this thread should be posted in the general support section.

    EDIT: Clarification
     
    Last edited: Feb 20, 2017
  3. DarkNeo

    DarkNeo

    Joined:
    Apr 27, 2014
    Posts:
    53

    I have tested this code and it simply unlocks the next level as you click on your first one and so on.

    It works great, but I cannot get this to work so when you enter level 2 it unlocks the level 2 button. Simply by clicking the level 1 button it automatically unlocks level 2 for you.

    I am still trying to configure it so as the character enters level 2 it then unlocks the level 2 button.
     
  4. rawegames

    rawegames

    Joined:
    Aug 1, 2016
    Posts:
    91
    Call the SaveLevel(id) function on the event when the player enters level 2, via code.
     
  5. LightingBox2

    LightingBox2

    Joined:
    Mar 31, 2015
    Posts:
    2,817
    Code (CSharp):
    1.     public GameObject loading;
    2.  
    3.     public GameObject[] levelLocks;
    4.  
    5.     void Start()
    6.     {
    7.  
    8.  
    9.         for (int a = 0; a < levelLocks.Length; a++) {
    10.  
    11.             if (PlayerPrefs.GetInt ("Level" + a.ToString () == 3))  // 3 => true , 0 => false
    12.                 levelLocks [a].SetActive (false);
    13.             else
    14.                 levelLocks [a].SetActive (true);
    15.  
    16.         }
    17.     }
    18.  
    19.     public void SelectLevel(int number)
    20.     {
    21.         if (PlayerPrefs.GetInt ("Level" + number.ToString () == 3)) {  // 3 => true , 0 => false
    22.             loading.SetActive (true);
    23.             Application.LoadLevel ("LevelName" + number.ToString ());
    24.         }
    25.     }
    0. Assign script to canvas.
    1. Drag level buttons lock icons respectively in LevelLocks slot.
    2. Call SelectLevel function from your level buttons in inspector(onClick from inspector).
    3. Set level number as number in SelectLevel function in inspector (0,1,2,3,4,5,n)
    4. Assign a UI Text or UI Image as loading game object to my script. its will be activated before calling LoadLevel function.

    If you want to unlock a new level call this function in end of your level:
    Code (CSharp):
    1.     void UnlockLevel(int number)
    2.     {
    3.         PlayerPrefs.SetInt ("Level" + number.ToString (), 3);  // 3 => true , 0 => false
    4.     }
    1.jpg
    21.jpg
     
  6. DarkNeo

    DarkNeo

    Joined:
    Apr 27, 2014
    Posts:
    53
    Thanks, I will try that at some stage. I figured out though that I can not call anything through a trigger. My touch controls for some reason are overriding all of this.
     
  7. Deleted User

    Deleted User

    Guest

    how will you save unlocking levels to binary formatter?
     
    Last edited by a moderator: May 23, 2018
  8. LightingBox2

    LightingBox2

    Joined:
    Mar 31, 2015
    Posts:
    2,817
    I don't know ...
     
  9. Abdul007

    Abdul007

    Joined:
    Mar 13, 2018
    Posts:
    3
    hi every one i am locking my levels with button interactive false every thing is working fine but when i unlock the next level every thing work fine after unlocking the next level when i play previous levels again upcoming level become false
    here is my code if anybody can help please

    if(assaultParent.transform.GetChild (PlayerPrefs.GetInt ("levelassault")+1).GetComponent<Button> ().enabled== false){
    print ("level locked"+assaultParent.transform.GetChild (PlayerPrefs.GetInt ("levelassault")+1).name);
    assaultLevelUnlocking = PlayerPrefs.GetInt ("levelassault");
    for (int i = 0; i <=assaultLevelUnlocking; i++) {
    assaultParent.transform.GetChild (i).GetComponent<Button> ().enabled= true;
    assaultParent.transform.GetChild (i).GetComponent<Image> ().color = Color.white;
    }
    }
     
  10. AndrijaPC

    AndrijaPC

    Joined:
    Jun 2, 2017
    Posts:
    1
    Can you please explain how to do that. I've been trying for hours but I can't get it to work.
     
  11. rahulpatil6220375

    rahulpatil6220375

    Joined:
    Dec 10, 2018
    Posts:
    19
    @AndrijaPC @LightingBox2 if (PlayerPrefs.GetInt ("Level" + a.ToString () == 3)) this line generate a error how to solve this error, i m working on 2d game and implement level lock and unlock system i mfacing this issue??thanks for answers
     
  12. mentolatux

    mentolatux

    Joined:
    Nov 2, 2014
    Posts:
    236
    i have same erro , we still learning code haha
     
  13. ishtiaqwajid

    ishtiaqwajid

    Joined:
    Oct 16, 2019
    Posts:
    2
    it is working like if we play level 1 there has unlock level 2 same as for level 3 but after it if we play level 1 again it maked lock level 3 and only unlock level 2...
    Why?
     
unityunity