Currently you are saving only one key, which is hasBought[ i], and do so hasBought.Length-many times. Instead you want to save (âhasBoughtâ+i) and do that hasBought.Length-many times. So for each i, we create one key, ie hasBought0, hasBought1 and so on. Now we would actually save hasBought.Length-many different keys and values, which we can recover in the same fashion. It is worth mentioning, that you only need to save the bought skin in Buy(), since only that one changes. Saving the entire array is unnecessary there. I would probably do it OnApplicationQuit still, but technically we only ever need to save the one that got bought.
Code example for saving the entire array:
for(int i = 0; i < hasBought.Length; i++){
PlayerPrefs.SetInt("hasBought" + i, hasBought[i] ? 1 : 0); // using tertiary operator to determine 1 or 0
}
To then load them we do the opposite:
int length = PlayerPrefs.GetInt(BoolLength);
hasBought = new bool[length];
for(int i = 0; i < length; i++){
bool value = PlayerPrefs.GetInt("hasBought" + i, 0) > 0; // ie 1 = true and 0 = false
hasBought[i] = value;
}
As mentioned above, as far as saving goes, you should only need to save the actual bought skin when buying a skin. You dont really need to worry about, if for example the program crashes, and you thus only saved hasBought5, but not hasBought 0 to 4, since only the skin at index 5 was bought. This would still work, since PlayerPrefs.GetInt returns the default value of 0, if it cannot find the key, ie GetInt(âhasBought4â) would still return 0 = not bought. Or rather we set the default value to be 0 if it cannot find the key, which is the second parameter for those GetSomething methods of PlayerPrefs.
Last but not least, you may have noticed that i changed the way to set the value to 0 or 1. I did this for two reasons. I personally think this way is easier readable, but most importantly the way you did it does not work. You check hashBought == 1, which will never be true, since you are comparing a bool value to 1. You would first need to cast it to an int (which should be possible, did not check), use the previously mentioned Convert.ToInt32, or do it like i did with a tertiary operator.
Hope this clears your confusion. I guess i should have posted examples from the beginning, but itâs always a bit annoying to directly code here in the forum editor