Hi All,
I’m adding buttons dynamically to a custom scrolling panel I’ve written but it does not return the correct button index I press it always return 8 which is the number of buttons in Characters, anybody got a clue why?
Cheers
private void CreateButtons()
{
float mButtonHeight = (((RectTransform)this.transform).rect.height * (ButtonHeight * 0.5F));
if (Characters != null && Characters.Count > 0)
{
//int mIndex = 0;
for (int i = 0; i < Characters.Count; i++)
{
GameObject mObject = Instantiate(Prefab, Vector3.zero, Quaternion.identity, this.transform);
mObject.SetActive(false);
Characters[i].Button = mObject.GetComponentInChildren<ButtonComponent>();
Characters[i].Button.RectTransform.sizeDelta = new Vector2(mButtonWidth, mButtonHeight);
//Characters[i].Button.Title = Characters[i].Title;
Button mButton = mObject.GetComponent<Button>();
if (mButton != null)
{
//mButton.onClick.AddListener(() => Button_Click(i));
mButton.onClick.AddListener(delegate { Button_Click(i); });
//mIndex++;
}
}
}
}
private void Button_Click(Int32 index)
{
OnButtonClick(new ButtonClickEventArgs(index));
}