Level unlock doesn't work for level 1-2

In my level unlock script, there is a bit of a problem. it works fine on levels 2+ but when you complete level 1, it doesn’t unlock level 2 in the level select.
heres the scripts

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

public class LevelSelect : MonoBehaviour
{
    public Button[] levelButtons;

    private void Start()
    {
        int levelReached = PlayerPrefs.GetInt("levelReached", 1);

        for (int i = 0; i < levelButtons.Length; i++)
        {
            if(i + 1 > levelReached)
                levelButtons[i].interactable = false;

        }
    }
}
public void WinLevel()
    {
        Debug.Log("Level won");
        levelCompleteMenu.SetActive(true);
        gameplayGUIS.SetActive(false);
        movement.canMove = false;
        Debug.Log("Gameobjects turned on and off");
        if (SceneManager.GetActiveScene().buildIndex - 1 == PlayerPrefs.GetInt("levelReached"))
        {
            PlayerPrefs.SetInt("levelReached", PlayerPrefs.GetInt("levelReached") + 1);
            Debug.Log("Level " + PlayerPrefs.GetInt("levelReached").ToString() + " unlocked.");
        }
        //Debug.Log("Wow! You found the real end!");
    }

also the build index of level 1 is 2, the build index of level 2 is 3, etc

edit: everything in the winlevel function works up until the if statement, the debug before it appears in the console

You really should avoid splattering that same string all over creation. The code is virtually impossible to read and easy to make typo mistakes.

Instead, package it up:

Here’s an example of simple persistent loading/saving values using PlayerPrefs:

https://gist.github.com/kurtdekker/01da815d2dfd336a925ae38019c3a163

Useful for a relatively small number of simple values.

As for why it’s not behaving, try and get away from doing math all on one line (like line 8 above) and make some temporary variables so you can print them out and find out what’s going wrong. It’s obviously some kind of data or limits problem based on it working everywhere else.

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?

Knowing this information will help you reason about the behavior you are seeing.

You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

https://discussions.unity.com/t/839300/3

nevermind i fixed it, i just changed up the script a little

public void WinLevel()
    {
        Debug.Log("Level won");
        levelCompleteMenu.SetActive(true);
        gameplayGUIS.SetActive(false);
        movement.canMove = false;
        Debug.Log("Gameobjects turned on and off");
        int nextSceneLoad = SceneManager.GetActiveScene().buildIndex;
        if (nextSceneLoad > PlayerPrefs.GetInt("levelReached"))
        {
            PlayerPrefs.SetInt("levelReached", nextSceneLoad);
            Debug.Log("Level " + PlayerPrefs.GetInt("levelReached").ToString() + " unlocked.");
        }
        //Debug.Log("Wow! You found the real end!");
    }