So, I am using playerprefs to keep track of the current score in my game and I need the score to go up by one every time a player touches an object. Usually with playerprefs you set it just as a constant number, for example:
PlayerPrefs.SetInt("Score", 1);
but that just sets the score always as one, and this doesn't work:
PlayerPrefs.SetInt("Score", ++);
Does anyone know how to just raise the score by one?
Thanks.
2 Answers
2
PlayerPrefs.SetInt("Score", PlayerPrefs.GetInt("Score")+1);
Not sure how fast these functions are, and if you really want to update that everytime. Usually you'd only want to store/read the prefs if you start/load a game, or save a game, or exit the app. In all other cases you are probably better off with a simple variable in one of your scripts.
Usually with playerprefs you set it just as a constant number
I think you would almost never do that. Usually you set it from a variable.
PlayerPrefs.SetInt("Score", playerScore);
That should answer the rest of the question.
Why not
– anon42580617++PlayerPrefs.GetInt("Score")?I mean for the assignment part
– anon42580617GetInt() is a method, you cannot increment that, nor its return value.
– Wolfram