I am writing a script that passes 2 arrays into a menu creation method- the arrays hold the text and the models (which the buttons spawn) for the buttons that the menu will create. However, due to the fact that I would like to make the whole thing dynamic I need to set all the references to the gameobjects at runtime.
Below is the method which sets up the panel with buttons
public void Choice(string upgradeQuestion, /*List<UnityAction>*/ string[] upgradeText, GameObject[] buildings)
{
Button[] buttonList = null;
buttonList = new Button[upgradeText.Length];
modalPanelObject.SetActive(true);
for (int i = 0; i < upgradeText.Length; i++)
{
Button newbut = Instantiate(button);
Debug.Log(newbut);
newbut.transform.SetParent(buttonPanel.transform);
buttonList *= newbut;*
//Remove previous listeners
buttonList*.onClick.RemoveAllListeners();*
//Add Panelclose listener
AddOnClickListener(buttonList*, ClosePanel);*
//Add Custom Listener
Debug.Log(buildings*);*
buttonList_.onClick.AddListener(delegate { BuildingPlacer(buildings*); });
buttonList.GetComponentInChildren().text = upgradeText;
Debug.Log(“button created”);
}
this.question.text = upgradeQuestion;
this.iconImage.gameObject.SetActive(false);
foreach (Button b in buttonList)
{
b.gameObject.SetActive(true);
}
}
The problem is that
buttonList.onClick.AddListener(delegate { BuildingPlacer(buildings); });*
returns an IndexOutOfRangeException when the buttons are clicked. The arrays are not empty and are of the same size, the buttons get created but the issue lies when the onclick is assigned.
Any idea why i am getting an index out of range_