PlayerPrefs.SetInt with a field

hello guys,

i’m creating a character selection and i’ve added a system to buy the characters with credits ingame but i have a issue:
i would like to save with PlayerPrefs the bought characters, i tryed this:

PlayerPrefs.SetInt (ToString(character), unlockedCharacter[character]);
character is the number of the selected character

what i was expecting is that it set int to 0 if it isn’t bought and 1 if it’s bought.

but this isn’t working, i’m new to scripting with unity.

could someone help me please ?

What is “unlockedCharacter”? An array of ints?

yes

What does “it isn’t working” mean? Compile error? Run-time error? Silently fails to set the playerpref? Sets the playerpref to the wrong value? Transfers the contents of your bank account to a prince in Nigeria?

Tell us what it does, not one particular thing that it doesn’t do.

1 Like

actually the PlayerPrefs.SetInt doesn’t save arrays so i have a error when ido PlayerPref.GetInt
is there a way to save arrays in PlayerPrefs ?

Hey guys i have find a solution, i have found this link:
http://wiki.unity3d.com/index.php/ArrayPrefs2#C.23_-_PlayerPrefsX.cs

thank you for your replys

1 Like

But you didn’t save an array, you saved an integer with a key of ToString(character). I see that saving an array is conceptually what you’re trying to do, and there may be better ways of doing that, but that doesn’t explain what’s going wrong with your current attempt.

Did you call GetInt using the same key (i.e., ToString(character)) that you used when calling SetInt?

i don’t understand your question but i have found another solution, i used a for loop to save all my characters

for(int i = 0; i < maxChar; i++){
    print (unlockedChar[i]);
    PlayerPrefs.SetInt("cube_"+i , unlockedChar[i]);
}