using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GenerateUIButtons : MonoBehaviour
{
public GameObject buttonPrefab;
public GameObject parent;
public int numberOfButtons;
public float spaceBetweenButtons;
private Button[] buttons;
// Start is called before the first frame update
void Start()
{
buttons = new Button[7];
for (int i = 0; i < Rotate.names.Length; i++)
{
GameObject newButton = Instantiate(buttonPrefab);
newButton.name = Rotate.names[i];
newButton.transform.SetParent(parent.transform, false);
buttons[i] = newButton.GetComponent<Button>();
buttons[i].onClick.AddListener(() => ButtonClicked(i));
}
}
void ButtonClicked(int buttonNo)
{
Debug.Log("Clicked On " + buttons[buttonNo]);
}
// Update is called once per frame
void Update()
{
}
}
Я получаю исключение в строке:
Debug.Log("Clicked On " + buttons[buttonNo]);
IndexOutOfRangeException: индекс находится за пределами массива
Что я хочу сделать, это когда я нажимаю наодна из кнопок будет выполнять те же действия внутри ButtonClicked.