C# Add Unique Listener to Buttons Instantiated from List
I have a list of instantiated buttons. On button click for each, I want to 1) update a scriptable object boolean, and 2) move the instantiated button from one list to the other (add/remove). However, I am not able to uniquely identify the buttons after instantiation. - I have a master array of scriptable objects (rpg items). - I loop through the items and add each to the appropriate list (storage or equipped). - For each item, I instantiate a button in the appropriate scrollrect. This (above) all works. Where it breaks down is on adding a listener to the instantiated buttons. In the StoreItem() and EquipItem() functions, I am not able to access the unique scriptable objects in order to update the boolean and move from one list to another. Rather, I am only able to add generic functions, such as the Debug.Log that you see here. Any thoughts on how to adjust?
// THE SCRIPTABLE OBJECT
using UnityEngine;
[CreateAssetMenu(fileName = "Item")]
public class InventoryItemModule : ScriptableObject
{
public string ItemName;
public bool isStorage;
public bool isEquippped;
public Sprite ItemImage;
}
// THE SCRIPT
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Inventory : MonoBehaviour
{
public InventoryItemModule[] masterInventoryItems;
public GameObject inventoryPanel;
public GameObject button;
public List<InventoryItemModule> storageInventoryItems = new List<InventoryItemModule>();
public List<InventoryItemModule> equippedInventoryItems = new List<InventoryItemModule>();
public Transform storageItemsList;
public Transform equippedItemsList;
public void DisplayFullInventory()
{
if (!inventoryPanel.activeInHierarchy)
{
inventoryPanel.SetActive(true);
foreach (InventoryItemModule inventoryItem in masterInventoryItems)
{
if (inventoryItem.isStorage)
{
storageInventoryItems.Add(inventoryItem);
}
if (inventoryItem.isEquippped)
{
equippedInventoryItems.Add(inventoryItem);
}
}
foreach (InventoryItemModule item in storageInventoryItems)
{
GameObject newButton = Instantiate(button);
newButton.transform.SetParent(storageItemsList, false);
newButton.GetComponentInChildren<Text>().text = item.ItemName;
newButton.GetComponent<Button>().onClick.AddListener(EquipItem);
}
foreach (InventoryItemModule item in equippedInventoryItems)
{
GameObject newButton = Instantiate(button);
newButton.transform.SetParent(equippedItemsList, false);
newButton.GetComponentInChildren<Text>().text = item.ItemName;
newButton.GetComponent<Button>().onClick.AddListener(StoreItem);
}
}
else if (inventoryPanel.activeInHierarchy)
{
storageInventoryItems.Clear();
equippedInventoryItems.Clear();
inventoryPanel.SetActive(false);
}
}
void StoreItem()
{
// UPDATE BOOLEAN (isStored or isEquipped)
// MOVE TO EquippedInventoryItems LIST
Debug.Log("Store Item");
}
void EquipItem()
{
// UPDATE BOOLEAN (isStored or isEquipped)
// MOVE TO StorageInventoryItems LIST
Debug.Log("Equip Item");
}
}
Your answer
Follow this Question
Related Questions
Prevent spawning if the gameobject has same position as a list of Vector3s using another list? 1 Answer
Add Clones of GameObject to List(array) 0 Answers
Having trouble with instantiating bullets at multiple points via a list of transforms. 1 Answer
Check if a large number of gameobjects are colliding 1 Answer