Some time ago I implemented unlockable rewards in my game (skins and coins you can spend in the shop), for testing purposes I only had 2 rewards. To check if the player have or not unlocked the rewards, I made a boolean in my “save” class:
public bool[] unlockableSkins = new bool[] { false, false, false, false, false } ;
// When I had only 2 rewards the bool length was 2 too ( { false, false};)
These rewards are progressive, and the rewards which give you a skin are displayed in the shop, managed inside my shop script:
private void OnSkinSelect (int currentIndex) {
//Selecting a skin, to check if you own it or not.
selectedSkinIndex = currentIndex;
if (SaveManager.Instance.IsSkinOwned(currentIndex)) {
//skin is owned
skinBuySetText.text = "Equip";
}
else {
if (skinCost[currentIndex] == 111) {
if (SaveManager.Instance.state.unlockableSkins[0] == false) {
skinBuySetText.text = "Play more to unlock";
}
if (SaveManager.Instance.state.unlockableSkins[0] == true) {
skinBuySetText.text = "Equip";
}
}
if (skinCost[currentIndex] == 112) {
if (SaveManager.Instance.state.unlockableSkins[2] == false) {
skinBuySetText.text = "Play more to unlock";
}
if (SaveManager.Instance.state.unlockableSkins[2] == true) {
skinBuySetText.text = "Equip";
}
}
if (skinCost[currentIndex] == 113) {
if (SaveManager.Instance.state.unlockableSkins[4] == false) {
skinBuySetText.text = "Play more to unlock";
}
if (SaveManager.Instance.state.unlockableSkins[4] == true) {
skinBuySetText.text = "Equip";
}
}
if (skinCost[currentIndex] == 121) {
skinBuySetText.text = "Highscore: 20";
}
else {
//skin isnt owned
skinBuySetText.text = "Buy " + skinCost[currentIndex].ToString();
}
}
}
The issue is, when I had only two rewards, it worked fine ( the only if statement checking the status of selected skin was the first (if (skinCost[currentIndex] == 111), the second reward bool unlocked coins.)
Now it seems it doesnt update the array length of my unlockableSkins bool, I added a debug.log when I open the shop that displays the length of the array, it says it still 2. I checked all my scripts and Inspector to see if the length was overwritten during runtime changing it back to 2, but I couldnt find anything.
So for whatever reason I think its serialized at array length 2 but I doesnt serialize it again at 5.
So when I click in the selected skin that checks the if (SaveManager.Instance.state.unlockableSkins[any value over 1]) it gives me an IndexOutOfRangeException: Array index is out of range.