- Home /
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!
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'
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:)
Forgot to add that the second method will work with all datatypes (not only int)
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.
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.).
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.
Also the data is in one of my own Classes if that makes a difference. (Wasn't letting me edit my last comment)
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.
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
}
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
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.
use
Inventory[i] = int.Parse(tempArray[i]);
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?
Certainly, I updated the code in my second post to show what I have now. I'm not getting the $$anonymous$$issing $$anonymous$$ethod 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'
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
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
Your answer
![](https://koobas.hobune.stream/wayback/20220613064400im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Why does this script freeze Unity 2 Answers
Problem with array 2 Answers
stack by quanity...? C# 2 Answers
How to make a highscore list? 0 Answers