I have a list of Buttons that get activated after certain conditions are met. I then add a listener to each button to call a function when clicked. That function then needs to be able to get the text off the button that was clicked. This is what I've tried but it returns an out of range error, I think because it calls the function once clicked, and at that point the variable i is maxed out.
public List<GameObject> ButtonList = new List<GameObject>();
void Start() {
for (int i = 0; i < ButtonList.Count; i++) {
ButtonList[i].SetActive(true);
ButtonList[i].GetComponent<Button>().onClick.AddListener(() => {GetButtonText(ButtonList[i]); });
}
}
public void GetButtonText(GameObject SelectedButton) {
Debug.Log(SelectedButton.GetComponentInChildren<TMP_Text>().text)
}
I thought about creating game objects for each button and assigning them in the inspector, but the number of buttons in the list is likely to fluctuate, so it needs to remain dynamic. Additionally, because the game object this script is attached to is a prefab, I can't set up the listeners in the inspector either.