I need help fixing a script to unlock levels and save the data

using UnityEngine;
using UnityEngine.SceneManagement;

public class LevelScript : MonoBehaviour
{
private const string LevelsUnlockedKey = “levelsUnlocked”;

public void Pass()
{
    int currentLevel = SceneManager.GetActiveScene().buildIndex;
    Debug.Log("Current Level Index: " + currentLevel);

    int levelsUnlocked = PlayerPrefs.GetInt(LevelsUnlockedKey, 1);
    Debug.Log("Levels Unlocked before update: " + levelsUnlocked);

    // Unlock the next level only if currentLevel is higher than previously unlocked levels
    if (currentLevel >= levelsUnlocked)
    {
        PlayerPrefs.SetInt(LevelsUnlockedKey, currentLevel + 1);
        PlayerPrefs.Save();
        Debug.Log("Levels Unlocked updated to: " + PlayerPrefs.GetInt(LevelsUnlockedKey));
    }
    else
    {
        Debug.Log("No update to levelsUnlocked, currentLevel: " + currentLevel + " < levelsUnlocked: " + levelsUnlocked);
    }
}

}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class LevelManager : MonoBehaviour
{
int levelsUnlocked;

public Button[] buttons;

private void Start()
{
    levelsUnlocked = PlayerPrefs.GetInt("LevelsUnlocked",1);

    levelsUnlocked = Mathf.Clamp(levelsUnlocked, 1, buttons.Length);

    for (int i = 0; i > buttons.Length; i++)
    {
        buttons[i].interactable = false;
    }

    for (int i = 0; i > levelsUnlocked; i++)
    {
        buttons[i].interactable = true;
    }

    Debug.Log("Levels Unlocked: " + levelsUnlocked);

}

public void LoadLevel(int LevelIndex)
{
    SceneManager.LoadScene(LevelIndex);

}

private void OnEnable()
{
    // Log PlayerPrefs when this script is enabled (e.g., returning to menu)
    int levelsUnlocked = PlayerPrefs.GetInt("LevelsUnlocked", 1);
    Debug.Log("Levels Unlocked when returning to menu: " + levelsUnlocked);
}

// Saves PlayerPrefs on application quit or pause
private void OnApplicationQuit()
{
    PlayerPrefs.Save();
}

private void OnApplicationPause(bool pauseStatus)
{
    if (pauseStatus)
    {
        PlayerPrefs.Save();
    }
}

}

Sounds like you wrote a bug… and that means… time to start debugging!

By debugging you can find out exactly what your program is doing so you can fix it.

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

As you learn more, if it is still mysterious, try this handy template:

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

This is the bare minimum of information to report:

  • what you want
  • what you tried
  • what you expected to happen
  • what actually happened, log output, variable values, and especially any errors you see
  • links to actual Unity3D documentation you used to cross-check your work (CRITICAL!!!)

The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven’t put effort into finding the documentation, why should we bother putting effort into replying?

You need to describe what the problem is, what you have tried, and what isn’t working as expected. Unfortunately we’re not yet able to read minds through computer screens.

Please don’t use the 2D tags unless you’re specifically asking about 2D features. This is a generic scripting question so the “Scripting” tag is more appropriate.

I’ll go ahead and remove them.

Thanks.