- Home /
Is there a way to add one to playerprefs?
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.
Answer by Wolfram · Aug 08, 2010 at 07:54 PM
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.
GetInt() is a method, you cannot increment that, nor its return value.
Answer by Eric5h5 · Aug 08, 2010 at 09:15 PM
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.
That was just how I was doing a lot of my other settings.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Is it necessary to create an Array() to show high scores in PlayerPref? 1 Answer
How many different scripts do I need to write to create a highscore system? 1 Answer
Where am I going wrong with these scripts? 1 Answer
What am I doing wrong with PlayerPrefs? 3 Answers