PlayerPrefs to make unlockable levels (Problem with unlocking next level after level is completed)

I created the whole menu and 2 levels in my game. All menus (OptionsMenu, LevelSelectMenu …) are in the same scene. I wrote a script that greyed out levels that are not unlocked yet, the first level is always unlocked. To unlock the next level you must complete the previous one. To complete the level you must hit the gate block with the player block. Before adding LevelSelectMenu, the transition to the next level looked like this (the script is assigned to the gate block):

public class ChangeLevel : MonoBehaviour
{
    void OnCollisionEnter(Collision col)
    {
        {
            Debug.Log("LEVEL COMPLETED");
            SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
        }
    }
}

After adding LevelSelectMenu and that after completing the level the next level unlocks, my script (assigned to the gate block) looks like this:

public class ChangeLevel : MonoBehaviour
{
    public string nextLevel = "Level2";
    public int levelToUnlock = 2;

    public SceneFader sceneFader;

    void OnCollisionEnter(Collision col)
    {
        {
            Debug.Log("LEVEL COMPLETED");
            SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
        }
    }

    public void WinLevel()
    {
        Debug.Log("LEVEL COMPLETED");
        PlayerPrefs.SetInt("levelReached", levelToUnlock);
        sceneFader.FadeTo(nextLevel);
    }

}

My LevelSelector script looks like this:

public class LevelSelector : MonoBehaviour
{
    public SceneFader sceneFader;

    public Button[] levelButtons;

    void Start ()
    {
        int levelReached = PlayerPrefs.GetInt("levelReached", 1);
        
        for (int i = 0; i < levelButtons.Length; i++)
        {
            if (i + 1 > levelReached)
                levelButtons*.interactable = false;*

}
}

public void Select(string levelName)
{
sceneFader.FadeTo(levelName);
}
}
My problem is that after passing level 1 it actually takes the player to the next level, but when I use the back button and go to the Level Selector tab, level 2 is still blocked and cannot be clicked.
Please help :slight_smile:

Do you reload the menu scene when you go back to the level Selector tab ?


Since the desactivation of your level selection buttons is on start, it should be restarted.