- Home /
How to lock an int as a playerPref?
I have a system that unlock in the menu the next level:
private void OnTriggerEnter2D(Collider2D col)
{
if (col.tag == "LadyBug")
{
FindObjectOfType<LevelManager>().NextLevel();
int unlock = SceneManager.GetActiveScene().buildIndex;
PlayerPrefs.SetInt("leavelCompleted", unlock + 1);
}
}
but if i am at level 10 and go back to play level 3, it will ulock level 4 and lock the next levels that a pass already, how can i prevent this to happen, if i had passed some level, he could never be locked again.
this is the code in the menu that unlock the nextlevel:
void Start()
{
leavelCompleted = PlayerPrefs.GetInt("leavelCompleted", 1);
for (int i = 0; i < levelButtons.Length; i++)
{
if (i + 1 > leavelCompleted)
{
levelButtons[i].interactable = false;
}
}
}
thanks!!
Answer by mbro514 · Jul 24, 2020 at 06:01 PM
Try this code for saving your game:
private void OnTriggerEnter2D(Collider2D col)
{
if (col.tag == "LadyBug")
{
FindObjectOfType<LevelManager>().NextLevel();
int unlock = SceneManager.GetActiveScene().buildIndex;
if (unlock + 1 > PlayerPrefs.GetInt("leavelCompleted"))
{ PlayerPrefs.SetInt("leavelCompleted", unlock + 1); }
}
}
Your answer
Follow this Question
Related Questions
Trying to find the highest number than add it to itself. 2 Answers
Point Counter Works Only Once! 1 Answer
How do I make do I make a variable go back to its original value for a spell system 1 Answer
how to allow the key to only open 1 door rather than all of them? 0 Answers
Why when using get; set; in one script i'm getting null in other script that use it ? 1 Answer