I am new to unity and I have finished my first game, but I am stuck in level unlock system. I have created this level unlock system, but the problem is whenever I have unlocked more than 2 or all the levels and I go back and play the first level, then all the other levels become locked again. Can anyone please help me. Code (CSharp): public int LeveltoUnlock = 2; public void Levelwon() { PlayerPrefs.SetInt("levelReached", LeveltoUnlock); } Code (CSharp): 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; } }
That's because you're not storing the highest level reached; you're storing the last level finished (which you nonetheless call "levelReached"). To make your variable name accurate, you only want to change it to a higher value, never a lower one: Code (CSharp): public void Levelwon() { if (PlayerPrefs.GetInt("levelReached") < LeveltoUnlock) { PlayerPrefs.SetInt("levelReached", LeveltoUnlock); } }
Deleted bogus poll. Please don't do that. Moved to scripting. Try to put in more effort when asking for help and you'll get better responses.