confused with playerprefs.

ive been trying to figure this out for days… what its SUPPOSED to do is check if the level that is reached is lower than a levels ‘checker’. if it is, it will set the level reached to a higher number. this code is just a big honkin mess. see it for yourself:

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

public class WinLevel : MonoBehaviour {

    public Text WinText;
    public Button WinButton;
    public Text ButtonText;
    public string LevelUnlocked = "Level2";
    public int LevelToUnlock = 2;
    int LevelReached = PlayerPrefs.GetInt("LevelReached");
    public int Checker;
    public Button[] levelButtons;
    public Text[] levelTexts;
    public Text[] ButtonTexts;

    // Use this for initialization
    void Start()

        for (int i = 0; i < levelButtons.Length; i++)
            int LevelReached = PlayerPrefs.GetInt("LevelReached");

            if (i + 1 > LevelReached)
                levelButtons*.enabled = false;*

levelTexts*.enabled = false;*
ButtonTexts*.enabled = false;*



void OnTriggerEnter(Collider other)
if (other.CompareTag(“FinishLine”))
WinText.enabled = true;
WinButton.enabled = true;
ButtonText.enabled = true;

if ( Checker > LevelReached)
PlayerPrefs.SetInt(“LevelReached”, LevelToUnlock);
Debug.Log(“Level Unlocked!”);

the part that’s actually not working Is the playerprefs. it wont let me use the LevelReached inside the Start method, so I tried to copy the same playerpref and hope it would register as the same thing. what happens is that I can progress through my levels nicely, but if I go back and play the first level, it deletes my progress.


In the ‘Start’ function, you are re-declaring a variable named ‘LevelReached’
int LevelReached = PlayerPrefs.GetInt(“LevelReached”);
remove the keyword ‘int’, so it should be:
LevelReached = PlayerPrefs.GetInt(“LevelReached”);

Also initialise ‘Checker’ in the code.