Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by awts202 · Mar 04, 2019 at 07:22 PM · listplayerprefsstrings

PlayerPrefs. Adding multiple strings on a List and saving it on PlayerPrefs.

Hello guys, I really need your help. I'm stuck. I'm saving my string to PlayerPrefs but the strings on index 3 will overwrite and causes Out of Range error when I'm saving it again. Here's my code.(Updated)

 private void BallSkinSave()
 {
     if (PlayerPrefs.HasKey("skinCount"))
     {
         Debug.Log("already exist  " + skinCount);
         getSkinCount = PlayerPrefs.GetInt("skinCount");
         getSkinCount++;
         PlayerPrefs.SetInt("skinCount", getSkinCount);
         for (int i = 0; i < getSkinCount; i++)
         {

             mySkins.Add(skinObject.gameObject.name);    
             PlayerPrefs.SetString("skinCollections" + (getSkinCount -1), mySkins[i+(getSkinCount - 1)]);
         }
         getSkinCount++;

     }
     else
     {
         Debug.Log("first time buying");
         skinCount++;
         mySkins.Add(skinObject.gameObject.name);
         PlayerPrefs.SetInt("skinCount", skinCount);
         PlayerPrefs.SetString("skinCollections" + 0, mySkins[0]);

     }

     PlayerPrefs.Save();
 }

I'm almost close to done. The problem left now is that my int i is set to 1 when there are no values. Please help. Anything is appreciated, Thanks in Advance,

Update: This still doesn't work. Even if 1 session only since mySkins[i] doesn't have any values yet.

Comment
Add comment · Show 2
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 ShadyProductions · Mar 06, 2019 at 02:42 PM 0
Share

Why don't you just use a dictionary with key skinIndex and value the skinName ins$$anonymous$$d of indexing on the collection?

I don't understand the for loop, why do u wanna add duplicates of the same skin into your mySkins list?

avatar image RobAnthem · Mar 06, 2019 at 03:25 PM 0
Share

It should just be as easy as

 public List<string> objectIDs;
 private const string objectIDHash = "objID";
 public void SaveIDs()
 {
     for (int i = 0; i < objectIDs.Count; i++)
     {
         PlayerPrefs.SetString(objectIDHash + i.ToString(), objectIDs[i]);
     }
     PlayerPrefs.SetInt(objectIDHash, objectIDs.Count);
 }
 public List<string> GetIDs()
 {
     List<string> loadedIDs = new List<string>();
     for (int i = 0; i < PlayerPrefs.GetInt(objectIDHash); i++)
     {
         loadedIDs.Add(PlayerPrefs.GetString(objectIDHash + i.ToString()));
     }
     return loadedIDs;
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Tsaras · Mar 04, 2019 at 08:03 PM

It is hard to understand exactly what you want to do with your code, but I can see some immediate flaws:

 for (int i = 1; i < getSkinCount; i++)
  {
      mySkins.Add(skinObject.gameObject.name);
      PlayerPrefs.SetString("skinCollections" + (getSkinCount - 1), mySkins[i]);
  }

Why are you starting the iteration at i=1 and not 0? And also what you do in the loop is overwriting the value of string "skinCollections" + (getSkinCount - 1) which is constant throughout the looping with mySkins[i].

Comment
Add comment · 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

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

109 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

save string from array to player prefs 1 Answer

lists and player prefs adding more than one entry 1 Answer

Why is this not saving to player prefs and loading (Works in editor only) 0 Answers

Textbox code not working 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