Search Unity

Unlocked levels relocking

Discussion in '2D' started by iningomontoya, Oct 19, 2017.

  1. iningomontoya

    iningomontoya

    Joined:
    Jul 27, 2017
    Posts:
    21
    I am working on a game that will have a level lock and unlock system. Now I realize that most people will not choose to go back and replay a level that they have already passed through. But I have noticed a glitch with my games level lock and unlock system where if you go back and play a previous level and don't win or if the game gets closed then the program will lock the next level again which had been previously unlocked. I tried moving the level unlocking into a load screen that would work between the levels to prevent this from happening but that did not work. I am not sure why. If anyone has any ideas on how to fix this it would be greatly appreciated.

    Here is the code for my Main Menu Script and Level Control Script below:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.SceneManagement;
    6.  
    7. public class MainManuControlScript : MonoBehaviour {
    8.  
    9.     public Button level02Button, level03Button;
    10.     int levelPassed;
    11.  
    12.     // Use this for initialization
    13.     void Start () {
    14.         levelPassed = PlayerPrefs.GetInt ("LevelPassed");
    15.         level02Button.interactable = true;
    16.         level03Button.interactable = false;
    17.  
    18.         switch (levelPassed) {
    19.         case 1:
    20.             level02Button.interactable = true;
    21.             break;
    22.         case 2:
    23.             level02Button.interactable = true;
    24.             level03Button.interactable = true;
    25.             break;
    26.         }
    27.     }
    28.    
    29.     public void levelToLoad (int level)
    30.     {
    31.         SceneManager.LoadScene (level);
    32.     }
    33.  
    34.     public void resetPlayerPrefs()
    35.     {
    36.         level02Button.interactable = false;
    37.         level03Button.interactable = false;
    38.         PlayerPrefs.DeleteAll ();
    39.     }
    40. }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.SceneManagement;
    6.  
    7. public class LevelControlScript : MonoBehaviour {
    8.  
    9.     public static LevelControlScript instance = null;
    10.     GameObject levelSign, gameOverText, youWinText;
    11.     int sceneIndex, levelPassed;
    12.  
    13.     // Use this for initialization
    14.     void Start () {
    15.        
    16.         if (instance == null)
    17.             instance = this;
    18.         else if (instance != this)
    19.             Destroy (gameObject);
    20.  
    21.         levelSign = GameObject.Find ("LevelNumber");
    22.         gameOverText = GameObject.Find ("GameOverText");
    23.         youWinText = GameObject.Find ("YouWinText");
    24.         gameOverText.gameObject.SetActive (false);
    25.         youWinText.gameObject.SetActive (false);
    26.  
    27.         sceneIndex = SceneManager.GetActiveScene ().buildIndex;
    28.         levelPassed = PlayerPrefs.GetInt ("LevelPassed");
    29.                 PlayerPrefs.SetInt ("LevelPassed", sceneIndex);
    30.     }
    31.  
    32.     public void youWin()
    33.     {
    34.         if (sceneIndex == 3)
    35.             Invoke ("loadMainMenu", 1f);
    36.         else {
    37.             if (levelPassed < sceneIndex)
    38.                 PlayerPrefs.SetInt ("LevelPassed", sceneIndex);
    39.             //levelSign.gameObject.SetActive (false);
    40.             youWinText.gameObject.SetActive (true);
    41.             Invoke ("loadNextLevel", 1f);
    42.                        
    43.         }
    44.     }
    45.  
    46.     public void youLose()
    47.     {
    48.         //levelSign.gameObject.SetActive (false);
    49.         gameOverText.gameObject.SetActive (true);
    50.         //Invoke ("loadMainMenu", 1f);
    51.     }
    52.  
    53.     void loadNextLevel()
    54.     {
    55.         SceneManager.LoadScene (sceneIndex + 1);
    56.     }
    57.  
    58.     void loadMainMenu()
    59.     {
    60.         SceneManager.LoadScene ("MainMenu");
    61.     }
    62.  
    63.      
    64. }
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    So it looks like you're keeping an integer "LevelPassed" with the highest sceneIndex stored in it.

    You have a line in your LevelControlScript's Start function that sets LevelPassed to the current index, which probably shouldn't happen, considering you only just started the level.
     
  3. iningomontoya

    iningomontoya

    Joined:
    Jul 27, 2017
    Posts:
    21
    I added this if statement.

    Code (CSharp):
    1. if ( PlayerPrefs.GetInt ("LevelPassed") < sceneIndex)
    right before

    Code (CSharp):
    1.  PlayerPrefs.SetInt ("LevelPassed", sceneIndex);
    I am very new at this but that seems to have corrected the problem.

    If I understand the code properly and that may be a big if, I think the code is setting LevelPassed at the start of the level which may be why the author of this script set it to automatically move you onto the next level after you win.