Problem with saving the level progress

Hi all,

I have a problem with saving the level progress in my game. After i closed the game (“Play” in unity) and open again all levels that i passed locked again. Im new with c# and unity engine i don"t understand why its not working.

This is the code:

public class LevelSelector : MonoBehaviour
{

public Button[] levelButtons;  //--Levels

void Start()
{
int levelReached = PlayerPrefs.GetInt("levelReached", 1);  //-- "1" = level 1 always opened

for(int i=0; i < levelButtons.Length; i++)
{
if(i+1 > levelReached)
{
levelButtons[i].interactable = false; //-- Disable the level button

}
}

}
}

I don’t see anything above that writes to PlayerPrefs. I only see the read on line 8.

Here’s a far better approach than splattering ugly "levelReached" crud all over your codebase and risking millions of typos.

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

Useful for a relatively small number of simple values.