- Home /
arrayPrefs playerPrefs
I don't know about anyone else out there, but understanding PlayerPrefs and arrays has me to the point of giving up! There is very little documentation on something seemingly critical to web play! YES, I've looked at the wiki ArrayPrefs, but I cannot understand it and when I attempt to copy/paste and retrofit, I get overload errors. With that said, I realize those examples are VERY simple, but for some maybe not so simple. For those who find it simple I would hope you would remember that UnityAnswers is for EVERYONE and that includes those who may not be near your level of expertise and require a little more info. So; with that out of the way, are there any simpathetic souls who can assist? I know PlayerPrefs for single pieces of info. What I need is how to create a PlayerPref with a single user, then add to that file with additional players/scores, then read back the file for future play.
Answer by Statement · Mar 09, 2011 at 02:25 PM
What you can do is have each player data prefix or suffix with a player index.
For example, if players have this data associated:
- Name
- Health
- Score
You could then support several player profiles as such:
- Name0
- Health0
- Score0
- Name1
- Health1
- Score1
- Name2
- Health2
- Score2
And you could keep track of how many players there are with another key
- PlayerCount
To select the name for player 1, you could do something like this:
var playerName : String = GetPlayerName(1);
function GetPlayerName(playerIndex : int) : String { var key : String = IndexedKey("Name", playerIndex); return PlayerPrefs.GetString(key); }
function IndexedKey(key : String, index : int) : String { return String.Format("{0}{1}", key, index); }
Note that you will need to move up entries if a player is deleted. For example, deleting player 1 needs all player 2 data to be stored in player 1 slots, and all player 3 data in player 2 slots, and so on. Otherwise you'll get "gaps" that you might have a hard time solving.
Thank you for keeping it simple, but stay with me; how are these names/scores set? I have a single textField and a single "Submit Name" button. Is "return String.Format("{0}{1}", key, index);" reading in the playerprefs file, and, further, how can I use that read-in info to then create "Play" buttons with the previously entered players names and scores? $$anonymous$$y aplogies if these questions are not being asked properly, but hopefull you'll understand and can help.
String.Format only formats a string. That is, it produces a string like "Name1". Then we use that string to be the key, that is used to access the data in player prefs. So we're calling PlayerPrefs.GetString("Name1"); basically. If you are willing to go one step further and make classes to help you maintain your data, you can make this completely transparent. Do you prefer C# or JS code? I could cook up a complete example.
Wouldn't it be necessary for me to read the playerprefs file each time the game starts to see if there are any entered players and, also, to be able to then add-on an additional players? It is the reading of and the assessing of the file contents that have me stumped.
Your answer
![](https://koobas.hobune.stream/wayback/20220613112010im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to save an array to PlayerPrefs? 1 Answer
bool array to playerprefs 1 Answer
Guide in ArrayPrefs or PlayerPrefx? 1 Answer
ArrayPrefs 1 Answer
Help with Saving ArrayPrefs 0 Answers