Problem with playerprefs (86698)

hey guys, im trying to add a value to a playerpref i already have, in this case +50, however the way ive been calling my playerprefs is like this

PlayerPrefs.GetInt("LevelPar1_" + Application.loadedLevelName)

i do this as i have many levels, but when i try to add to that playerpref, i get these errors

cannot convert int' expression to type string’
The best overloaded method match for `UnityEngine.PlayerPrefs.GetInt(string)’ has some invalid arguments

the line im using is this

enter code herePlayerPrefs.SetInt(PlayerPrefs.GetInt(Score), PlayerPrefs.GetInt(Score)+1);

score is set as the follwing

Score = PlayerPrefs.GetInt("LevelPar1_" + Application.loadedLevelName);

The line: PlayerPrefs.SetInt(PlayerPrefs.GetInt(Score), PlayerPrefs.GetInt(Score)+1); Is trying to set a player pref which is named whatever the player pref "Score"'s value is. I can't tell from your code but you probably want to be setting something with one constant name.

1 Answer

1

As the error message states, you are trying to access a PlayerPrefs property using an int, Score, as a key, but PlayerPrefs requires a string. From your code I’d assume you are trying to do this:

PlayerPrefs.SetInt("LevelPar1_" + Application.loadedLevelName, PlayerPrefs.GetInt("LevelPar1_" + Application.loadedLevelName)+1);

Or if you want to use the Score variable to increment the PlayerPrefs property:

PlayerPrefs.SetInt("LevelPar1_" + Application.loadedLevelName, Score+1);