I’ve been following this video and for some reason the items wont display in my inventory, but they have been picked up as they show up in my items array if any one knows how to fix this that would be greatly apricated.
code below.
public class InventoryManager : MonoBehaviour
{
public static InventoryManager instance;
public List<item> items = new List<item>();
public Transform content;
public GameObject inventoryitem;
private void Awake()
{
instance = this;
}
public void add(item item)
{
items.Add(item);
}
public void remove(item item)
{
items.Remove(item);
}
public void listitems()
{
foreach (var item in items)
{
GameObject obj = Instantiate(inventoryitem, content);
var itemName = obj.transform.Find("ItemName").GetComponent<TMPro.TextMeshProUGUI>();
var itemIcon = obj.transform.Find("itemIcon").GetComponent<Image>();
itemName.text = item.ItemName;
itemIcon.sprite = item.icon;
}
}
I see considerably more code in the video than I do in that snippet you’ve posted. If I had to guess you haven’t written the code that calls ListItems. You’re not following it exactly either. I see different capitalization in your methods. Little differences like this mean the tutorial may break in ways that are difficult to detect.
Speaking of the code seeing this in a loop just makes me cringe. It’s at least one of the faster variants of Find but the author never should have had it in the loop. The Find methods are extremely brittle and if you use it like this everywhere performance will be just awful.