Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed May 27, 2019 at 05:49 AM by Hellium for the following reason:

Duplicate of : https://answers.unity.com/questions/1376530/add-listeners-to-array-of-buttons.html

avatar image
0
Question by DevelopmentCompanyOfEntertainingGames · May 27, 2019 at 05:47 AM · errorargumentoutofrangeexception

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: alt text


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!

error.png (4.5 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kreaturian · May 27, 2019 at 05:12 AM 0
Share

Have you changed the amount of variables in the array in the component?

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

140 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges