How to save multiple data on Web
so for example i have 3 buttons which they have all the same script in it with variables of level,count,cost. how do i save their data individually using player prefs? so that i have for example(button1 > level = 1,count =2,cost = 3, button2 >level = 5,count = 10, cost = 9, button3 > level = 10, count = 3, cost = 100).
Answer by Dave-Carlile · Sep 03, 2015 at 03:46 PM
Include the button name in the player prefs key...
void SaveData(string name, int level, int count, int cost)
{
PlayerPrefs.SetInt(name + "_Level", level);
PlayerPrefs.SetInt(name + "_Count", count);
PlayerPrefs.SetInt(name + "_Cost", cost);
}
Call it something like this...
SaveData(button1.name, button1Level, button1Count, button1Cost);
SaveData(button2.name, button2Level, button2Count, button2Cost);
You'll have to get the individual values however you're normally doing that, but the key is to have each set of data start with a unique value.
Another way to do it would be to use JSON or some other structured data format. You'd build a JSON string that has all of the data and store it as a single value in PlayerPrefs...
Data: [
Button1: { Level: 1, Count: 2, Cost: 3 },
Button2: { Level: 5, Count: 10, Cost: 9 },
Button3: { Level: 10, Count: 3, Cost: 100 }
]
Your answer
![](https://koobas.hobune.stream/wayback/20220612002352im_/https://answers.unity.com/themes/thub/images/avi.jpg)