• 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
Question by ObviouslyInsane · Feb 23, 2012 at 09:15 AM · listsaveitemsforloop

How to save a list of items?

So I understand the basics of PlayerPrefs and how to save data, one thing I can't figure out though is how to save a list of items. For example: equipped items, items in inventory, etc. I was thinking a 'forloop' would work for this but so far I have not been successful. Can anyone give me an idea of how to go about this? Any input is much appreciated. Thank you!

Comment
Stardog

People who like this

1 Show 1
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 iteis · Oct 31, 2012 at 12:44 PM 0
Share

Has anyone found a solution to this yet? I have run into the same problem where I want to save a list of data, but I am getting the exact same error on the same line:

FormatException: Input string was not in the correct format'

4 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by flamy · Feb 23, 2012 at 12:31 PM

with playerprefs there are two ways to do it... one is using a for loop as you tried. Like the below

for(int i=0;i<TotalInventoryItem;i++)
{
    PlayerPrefs.SetString("InventoryItem"+i,"*insert your item value or name*");
}

even while retrieving you can do the same. This is one method..

But if the Items in inventory is an array or list!! you can easily save and retrieve it in this method. (unless number length or array is way too big!!)

for instance let the array name be Inventory

int[] Inventory; string temp="";

for(int i=0;i< Inventory.Length;i++) { if(i!=Inventory.Length-1) temp+=Inventory[i].ToString()+"*";//note that the last character you add //is important else temp+=Inventory[i].ToString(); }

PlayerPrefs.SetString("InventoryItems",temp);

While retriving you have to just get the InventoryItems key and slplit it and explode it back, like this

string temp=PlayerPrefs.GetString("InventoryItems"); string[] tempArray=temp.Split("*".ToCharArray());

for(int i=0;i<tempArray.length;i++) { Inventory[i]=System.Int32.Parse(tempArray[i]); }

Hope it is clear:)

Comment
MithosAnnar
Stardog
occak
Serhii-Horun
Tr1xo

People who like this

5 Show 2 · 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 flamy · Feb 23, 2012 at 12:34 PM 0
Share

Forgot to add that the second method will work with all datatypes (not only int)

avatar image ObviouslyInsane · Feb 23, 2012 at 09:36 PM 0
Share

Wow, thank you very much guys. You gave far more info that what I was even hoping for. I just woke up and haven't had a chance to try anything yet but I'm sure i'll have no problem figuring it out now.

avatar image

Answer by Eric5h5 · Feb 24, 2012 at 10:02 AM

Use ArrayPrefs2 (as long as it's arrays of basic types like Vector3, String, etc.).

Comment
mast3rillusion
Vagonn

People who like this

2 Show 4 · 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 ObviouslyInsane · Feb 24, 2012 at 10:44 AM 0
Share

Thank you Eric, I'm headed to bed now but I will definitely look into that in the morning. One more thing I think I should mention though now that I think of it is the items in the list I am trying to save contain both string and int values. Will using just SetStringArray or SetIntArray work for saving both values? Sorry if that's confusing I can't think straight anymore.

avatar image ObviouslyInsane · Feb 24, 2012 at 10:48 AM 0
Share

Also the data is in one of my own Classes if that makes a difference. (Wasn't letting me edit my last comment)

avatar image Eric5h5 · Feb 24, 2012 at 10:52 AM 0
Share

SetStringArray only works with String[] arrays, and SetIntArray only works with int[] arrays. You could use the techniques in that script to make a custom function, or you could convert the data in your classes to String[] and int[] arrays before saving, then convert the String[] and int[] arrays to your classes when loading.

avatar image andybak · Feb 17, 2018 at 04:21 PM 0
Share

Fixed link: http://wiki.unity3d.com/index.php/ArrayPrefs2

avatar image

Answer by ObviouslyInsane · Feb 24, 2012 at 05:34 AM

Ok I guess I can use a little more help. I should have mentioned I'm using JS and trying to save items in a List. Here's what I have so far:

 import System.Collections;
 var Inventory;
 var temp : String = "";
 var tempArray : String[];
 var i : int;
 
     //SAVE DATA 
      Inventory = GameObject.Find("GUIElements").GetComponent("Inventory").inventory;
 
     for(i = 0; i < Inventory.Count; i++) {
     
             if(i != Inventory.Count -1)
             temp += Inventory.ToString()+"*";
                                        
             else
             temp += Inventory.ToString();
             }
     
             PlayerPrefs.SetString("InventoryItems",temp);
     
 
     //LOAD DATA
      Inventory = GameObject.Find("GUIElements").GetComponent("Inventory").inventory;
 
         temp = PlayerPrefs.GetString("InventoryItems");
         tempArray = temp.Split("*".ToCharArray());
         
         for(i = 0; i < tempArray.Length; i++)
     {
       Inventory[i] = System.Int32.Parse(tempArray[i]); <<<ERROR HERE
     }
Comment
Chris12345

People who like this

1 Show 6 · 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 flamy · Feb 24, 2012 at 05:51 AM 0
Share

i made 2 typos there sorry in the first for loop... i have updated in my answer now and regarding missing method exception add

import System.Collections;

in the beginning of the file.... also while declaring use String, like

var temp:String; var tempArray:String[];

and in your last for loop condition it is tempArray.Length not Count

avatar image ObviouslyInsane · Feb 24, 2012 at 08:41 AM 0
Share

Ok, I changed everything as you said and added the import System.Collections but I am still getting that same error. In case it makes any difference I want to clarify that the variable (Inventory) I am referencing is a list and not an array.

avatar image Vagonn ObviouslyInsane · Nov 13, 2015 at 10:27 AM 0
Share

use

Inventory[i] = int.Parse(tempArray[i]);

avatar image flamy · Feb 24, 2012 at 09:03 AM 0
Share

hmm never had this problem it work fine for me... i copied ur code and even it works fine here... can copy the present code here?

avatar image ObviouslyInsane · Feb 24, 2012 at 09:57 AM 0
Share

Certainly, I updated the code in my second post to show what I have now. I'm not getting the Missing Method error anymore, but I am getting a new one at the line I marked. It says 'FormatException: Input string was not in the correct format'

avatar image flamy · Feb 24, 2012 at 10:54 AM 0
Share

for(i = 0; i < Inventory.Count; i++) {

         if(i != Inventory.Count -1)
         temp += Inventory[i].ToString()+"*";      <<this line

         else
         temp += Inventory[i].ToString();          <<this line
         }

         PlayerPrefs.SetString("InventoryItems",temp);


you have to add i there, is that done also try printing the temp vatiable when u save it and also when u retrive it

avatar image

Answer by toomas008 · Feb 23, 2012 at 04:13 PM

If the data that you're trying to serialize is in your own classes, then you may look into .NET serialization. But keep in mind, that it doesn't work when you want to store MonoBehaviours or other class instances from Unity namespaces, because deserialization tries to do "new" on MonoBehaviours and not Instantiate() as Unity requires.

A very tiny example of the concept: http://blog.kowalczyk.info/article/Serialization-in-C.html

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 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

Related Questions

A node in a childnode? 1 Answer

thousands of score coins to collect and thousands of items to collect gradually 1 Answer

How to save a list of MonoBehaviours in a binary file 0 Answers

Javascript Array to Generic List 1 Answer

I need to find inventory and item system, and to save them 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