0

I below code :

public async void GenerateCollectionData()
    {
        DataClassArray = new DataClass[bookCatogaryObject.Data.Rows.Count];
  
        for (int i = 0; i < bookCatogaryObject.Data.Rows.Count; i++)
        {
            DataClassArray[i] = new DataClass();
            DataClassArray[i].collectionDataClass = new CollectionDataClass[bookCatogaryObject.Data.Rows[i].String.Count];
            DataClassArray[i].categaryPrefabObject = bookCatogaryPrefabObjects[i];

            for (int m = 0; m < bookCatogaryObject.Data.Rows[i].String.Count; m++)
            {
                GameObject obj = DataClassArray[i].categaryPrefabObject.gameObject;

                GameObject childObj = GetChildGameObject(obj, "Content");

                GameObject collectionImageContainer = new GameObject();
                collectionImageContainer.AddComponent<Image>();
                collectionImageContainer.AddComponent<JMRUIPrimaryButton>();
                //collectionImageContainer.AddComponent<InteractionManager>();

                collectionImageContainer.transform.parent = childObj.transform;

                collectionImageContainer.GetComponent<Image>().sprite = 
                    Sprite.Create(
                        DataClassArray[i].collectionDataClass[m].collectionImage, 
                        new Rect(0.0f, 0.0f, DataClassArray[i].collectionDataClass[m].collectionImage.width, 
                        DataClassArray[i].collectionDataClass[m].collectionImage.height), 
                        new Vector2(0.5f, 0.5f)
                        );
                RectTransform setValues = collectionImageContainer.GetComponent<RectTransform>();

                setValues.localPosition = new Vector3(0f, 0f, 0f);
                setValues.localRotation = Quaternion.identity;
                setValues.localScale = new Vector3(1f, 1f, 1f);
               
                DataClassArray[i].collectionDataClass[m].collectionButton = collectionImageContainer;

                collectionImageContainer.GetComponent<JMRUIPrimaryButton>().
                    OnClick.AddListener
                    (
                        delegate () 
                        {
                            OnClickEvent(DataClassArray[i].collectionDataClass[m].id);
                        }
                    );
                //StartCoroutine(BookCollection(requestString, str));
            }

        }   
    }


    public void OnClickEvent(string tempString)
    {
        Debug.Log(tempString);
    }

Im trying to get _id associated to that specific button which is clicked When I execute the above code I get "IndexOutOfRangeException: Index was outside the bounds of the array." error at line OnClickEvent(DataClassArray[i].collectionDataClass[m].id); What might be the issue?

Omkar P
  • 29
  • 5
  • 2
    I think this is a case where the value of `i` is `bookCatogaryObject.Data.Rows.Count` by the time the loop has ended and all the handler delegates have been installed with `OnClick.AddListener`. By the time the click event occurs (long after `GenerateCollectionData` has returned) the value of `i` is out of range. The general approach for a solution is to make a copy of the loop variable `i` as shown [here](https://stackoverflow.com/questions/271440/captured-variable-in-a-loop-in-c-sharp) – Wyck Jun 23 '22 at 11:52
  • 1
    @Wyck Should i Make the copy of both variables (i & m) or only i variable? – Omkar P Jun 23 '22 at 12:01
  • 1
    It looks like both `i` and `m` need fixing up in this way. – Wyck Jun 23 '22 at 12:06

0 Answers0