- Home /
Duplicate of : https://answers.unity.com/questions/1376530/add-listeners-to-array-of-buttons.html
Error : Index was out of range.
Hello, I have a little problem. I took a script from a tutorial to make a shop that I modified :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class Shop : MonoBehaviour
{
public Inventory inventoryPlayer;
public CharacterMotor characterMotor;
public PlayerInventory playerinv;
public GameObject shopPanel;
public GameObject contentOfShopPanel;
public ItemDataBaseList itemDb;
public GameObject itemSlot;
[Header("ID des items du shop")]
public int numOfItem;
public List<int> itemId = new List<int>();
private int amountSlots;
private int slotsChecked;
private bool transactionDone;
// Tous les paramètres des objets du shop
public List<Item> theItem = new List<Item>();
public void Start()
{
shopPanel = GameObject.Find("Panel - Shop");
contentOfShopPanel = shopPanel.transform.GetChild(1).GetChild(0).GetChild(0).gameObject;
characterMotor = GameObject.FindObjectOfType<CharacterMotor>();
inventoryPlayer = GameObject.Find("Panel - Inventory(Clone)").GetComponent<Inventory>();
playerinv = GameObject.FindObjectOfType<PlayerInventory>();
shopPanel.SetActive(false);
}
// Préparer le shop
public void PrepareShop()
{
shopPanel.SetActive(true);
for (int i = 0; i < numOfItem; i++)
{
Instantiate(itemSlot, contentOfShopPanel.transform.position, contentOfShopPanel.transform.rotation, contentOfShopPanel.transform);
theItem.Add(itemDb.getItemByID(itemId[i]));
contentOfShopPanel.transform.GetChild(i).GetChild(0).GetComponent<TextMeshProUGUI>().text = theItem[i].itemName.ToString();
contentOfShopPanel.transform.GetChild(i).GetChild(3).GetComponent<TextMeshProUGUI>().text = theItem[i].itemWeight.ToString();
contentOfShopPanel.transform.GetChild(i).GetChild(5).GetComponent<TextMeshProUGUI>().text = theItem[i].itemValue.ToString();
contentOfShopPanel.transform.GetChild(i).GetChild(1).GetChild(0).GetComponent<Image>().sprite = theItem[i].itemIcon;
contentOfShopPanel.transform.GetChild(i).GetComponent<Button>().onClick.AddListener(delegate { BuyItem(theItem[i]); });
}
}
// Nettoyer tout le shop
public void CleanShop()
{
theItem.Clear();
foreach (Transform child in contentOfShopPanel.transform)
{
Destroy(child.gameObject);
}
}
// Acheter un item
public void BuyItem(Item finalItem)
{
amountSlots = inventoryPlayer.transform.GetChild(1).childCount;
transactionDone = false;
slotsChecked = 0;
foreach (Transform child in inventoryPlayer.transform.GetChild(1))
{
if (child.childCount == 0)
{
if (playerinv.florins >= finalItem.itemValue)
{
inventoryPlayer.addItemToInventory(finalItem.itemID);
playerinv.florins -= finalItem.itemValue;
transactionDone = true;
print("Le joueur a acheté l'objet : " + finalItem.itemName);
break;
}
else
{
print("Transaction refusée, le joueur n'a pas assez d'argent.");
}
}
slotsChecked++;
}
if (slotsChecked == amountSlots && transactionDone == false)
{
print("Transaction annulée, pas de place dans l'inventaire.");
}
}
// Détection d'entrée du joueur
public void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
PrepareShop();
characterMotor.isInShop = true;
}
}
// Détection de sortie du joueur
public void OnTriggerExit2D(Collider2D other)
{
if (other.tag == "Player")
{
CleanShop();
shopPanel.SetActive(false);
characterMotor.isInShop = false;
}
}
}
Everything works perfectly, except the purchase. It's ironic for a shop.
When I press one buttons to buy an item (so launch the "BuyItem" function), an error appears:
The error is on line 59.
contentOfShopPanel.transform.GetChild(i).GetComponent<Button>().onClick.AddListener(delegate { BuyItem(theItem[i]); });
It should work, I don't understand! Could you help me?
Sorry about my bad English, I'm French. Thank you in advance!
Have you changed the amount of variables in the array in the component?
Follow this Question
Related Questions
Scrolling inventory script giving argument out of range. 2 Answers
Versy confusing argumentoutofrangeexception error 1 Answer
Error building player when run on Asus Transformer Prime with android 4.0.3 3 Answers
The generic list blues (Argument out of range error) 1 Answer
Argument out of Range Exception Error - Parameter Name: Index 1 Answer