created an inventory system and while each item worked the button that uses the item would always be the same regardless of what item it was. to fix this I used this
public class InventoryManager : MonoBehaviour
{
public static InventoryManager Instance;
public List<InventoryItem> inventoryItems = new List<InventoryItem>();
public Transform itemContent;
public GameObject inventoryItemObject;
public void ListItems()
{
foreach (var InventoryItem in inventoryItems)
{
GameObject obj = Instantiate(inventoryItemObject, itemContent);
var item_name = obj.transform.Find("inventory/Item_name").GetComponent<Text>();
var item_icon = obj.transform.Find("inventory/Item_icon").GetComponent<Image>();
item_name.text = InventoryItem.item_name;
item_icon.sprite = InventoryItem.item_icon;
}
}
the issue is it doesn't work and I get the error: NullReferenceException: Object reference not set to an instance of an object InventoryManager.ListItems () (at Assets/Scripts/InventoryManager.cs:47)
the names Item_name and Item_icon do match with the varibles each item has
and the items are in the inventory folder
so I don't know how this isn't working