Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by steven pearce · Mar 09, 2011 at 02:12 PM · playerprefsarrayprefs

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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); }

Comment
Add comment · Show 18 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Statement · Mar 09, 2011 at 02:27 PM 0
Share

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.

avatar image Statement · Mar 09, 2011 at 02:32 PM 0
Share

Edit: Fixed typo in script.

avatar image steven pearce · Mar 09, 2011 at 02:59 PM 0
Share

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.

avatar image Statement · Mar 09, 2011 at 03:03 PM 0
Share

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.

avatar image steven pearce · Mar 09, 2011 at 03:05 PM 0
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges