I want to have a create a pop-up menu which lists the player’s inventory (as buttons) and when one is selected, that item is spawned.
To create the buttons I iterate over the player’s inventory but I’m not sure how to add a listener to each button so that the correct item is passed to the spawner class as a parameter?
I am also open to alternative methods of achieving this goal.
private void CreateButtons()
{
presentedButtons = new List<UnityEngine.UI.Button>();
for (int i = 0; i < inventoryToDisplay.Container.ItemLines.Count; i++)
{
presentedButtons.Add(Instantiate(ButtonPrefab, new Vector3(XStartPosition, YStartPosition - (i * YSpaceBetweenRows), 0), Quaternion.identity));
presentedButtons[i].transform.SetParent(GetComponent<RectTransform>(), false);
TMP_Text txt = presentedButtons[i].GetComponentInChildren<TMP_Text>();
txt.text = inventoryToDisplay.Container.ItemLines[i].item.Name + " (" + inventoryToDisplay.Container.ItemLines[i].amount + ")";
presentedButtons[i].onClick.AddListener(() => {
_spawner.testButtonPress(inventoryToDisplay.Container.ItemLines[i].item);
});
}
}