I am working on Unity 2D game. I used PlayerPrefs to save the level of the scene. As I go from one level to the next, the levels become unlocked and it works fine. But, after unlocking a few levels, when I go back to the lowest level, all the levels above become locked. What is the problem in my code?
Here is the LevelSelector
class:
public class LevelSelector : MonoBehaviour {
public void Update ()
{
{
int myLevel = PlayerPrefs.GetInt ("LevelPassed",1);
for (int level = 0; level < myLevel; level++)
{
levelButtons[level].interactable = true;
}
}
}
}
Here is the Score
script:
public class Score : MonoBehaviour {
void Update ()
{
UpdateScore ();
}
public void UpdateScore ()
{
if (sceneOne.isLoaded && meshLevel != null) {
zLevel = 1;
meshLevel.text = "Level " + zLevel;
PlayerPrefs.SetInt ("LevelPassed", 1);
}
else if (sceneTwo.isLoaded && meshLevel != null) {
zLevel = 2;
meshLevel.text = "Level " + zLevel;
PlayerPrefs.SetInt ("LevelPassed", 2);
}
else if (sceneThree.isLoaded && meshLevel != null) {
zLevel = 3;
meshLevel.text = "Level " + zLevel;
PlayerPrefs.SetInt ("LevelPassed", 3);
}
else if (sceneFour.isLoaded && meshLevel != null) {
zLevel = 4;
meshLevel.text = "Level " + zLevel;
PlayerPrefs.SetInt ("LevelPassed", 4);
}
}
}