- Home /
Integer gets 2 as a value istead of 1
So I have probably a really stupid problem that well I becouse I am a newbie can't figure out. Basically I want my code to add 1 to a PlayerPref that is saving how many level a player has finished but istead of 1 it adds 2 then 4 and so on
Here is the Code that I made :
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player") && win == true)
{
GameHUD.SetActive(false);
PlayerPrefs.SetInt("ClearedLevels", PlayerPrefs.GetInt("ClearedLevels") + 1);
the part that is important the rest work as it should
WinScreen.SetActive(true);
}
else
{
text.SetActive(true);
StartCoroutine(textdisable());
}
}
Answer by JonKach · Aug 30, 2020 at 01:11 PM
PlayerPrefs.SetInt("ClearedLevels",
PlayerPrefs.GetInt("ClearedLevels") + 1);
I’m also relatively new to Unity so don’t take my word on this why aren’t you saving the current level you’re on and then adding 1 in the SetInt function?
PlayerPrefs.SetInt("ClearedLevels", yourCurrentLevel + 1);
And then when you want to retrieve do this
PlayerPrefs.GetInt(“ClearedLevels”);
On your first Level make sure to set yourCurrentLevel to 0 so that it will add to 1.
Again, I’m sorry if this doesn’t work for you, this is how I set it up in my 3D games.
Your answer
Follow this Question
Related Questions
Why does monobehavior in if statement compiles? 1 Answer
Multiple Cars not working 1 Answer
How to change the values of an animation using a variable ??? 2 Answers
Distribute terrain in zones 3 Answers
Read in 2 Different Arduino Values 1 Answer