The array amount has 3 element element0 - 100, element1 - 200 and element2 - 300. when i click the claim button for the first time it add 100 coins which is correct but when i click second time it add 300 coins instead of 200 coins and when i click the third time it add 800 coins instead of 300 coins . And after adding 300 coins it shows index out of the range exception. why does it add previous index amount. for the first time it add 100 which is correct. for the second time it add 300 which is (element0 + element1) 100 + 200 and for the third time it add 800 which is (element0 + elemnent1 + element1 + element2) 100 + 200 + 200 + 300
[System.Serializable]
public class AchievementData
{
public int[] itemCount, amount;
public Button claimButton;
}
public class AchievementsUI : MonoBehaviour
{
public CashSystem cashSystem;
public AchievementData[] achievementData;
public List<string> savedName = new List<string>();
public List<int> savedCount = new List<int>();
void Start()
{
string path = Application.persistentDataPath + "/AchievementItemName.dat";
if (File.Exists(path))
{
DataManager8 data = SaveData.LoadAchievementItemName();
savedName = data.itemName;
DataManager9 count = SaveData.LoadAchievementItemCount();
savedCount = count.itemCount;
}
for(int i = 0; i < savedName.Count; i++)
{
switch(savedName*)*
{
case “Gold”:
Inititalize(0, savedCount_, savedName*);
break;
case “Silver”:
Inititalize(1, savedCount, savedName);
break;
}
}
}*_
void Inititalize(int id, int count)
{
for(int i = 0; i < achievementData[id].itemCount.Length; i++)
{
if(count >= achievementData[id].itemCount*)*
{
achievementData[id].claimButton.interactable = true;
achievementData[id].claimButton.onClick.AddListener(() => ClaimButtonClick(id, i, count));
break;
}
}
}
void ClaimButtonClick(int id, int index,int count)
{
cashSystem.AddCash(achievementData[id].amount[index]);
achievementData[id].claimButton.interactable = false;
AchievementStatus(id, index, count);
}
void AchievementStatus(int id, int index, int count)
{
index += 1;
if(count >= achievementData[id].itemCount[index])
{
achievementData[id].claimButton.interactable = true;
achievementData[id].claimButton.onClick.AddListener(() => ClaimButtonClick(id, index, count));
}
}
}