- Home /
This question was
closed Feb 20, 2019 at 12:11 PM by
panim0_unity for the following reason:
I found my mistake , it isn't about code
Question by
panim0_unity · Feb 20, 2019 at 10:56 AM ·
c#playerprefslevelslocking
Locking levels.
This is for locking
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;
levelButtons[i].image.color = Color.red;
}
}
}
These are from end of each levels: 1:
PlayerPrefs.SetInt("levelReached", 2);
2:
PlayerPrefs.SetInt("levelReached", 3);
3:
PlayerPrefs.SetInt("levelReached", 4);
So the problem is when i finish level 2, I see level 3 and 4 as unlocked and when i play 3rd level and finish it, it locks level 4 again. Can anyone see the reason?
Comment
Answer by mansoor090 · Feb 20, 2019 at 11:06 AM
public Button[] levelButtons;
private void Start()
{
int levelReached = PlayerPrefs.GetInt("levelReached",1);
for (int i=0; i <= levelButtons.Length; i++) {
if (levelReached > i) {
levelButtons[i].interactable = false;
levelButtons[i].image.color = Color.red;
}else{
levelButtons[i].interactable = true;
}
}
}
Here try this