need help with inventory system

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;

        }


    }

are you sure you set icons?

This sounds like a bug, perhaps a mistake in how you did the tutorial, perhaps something else.

Whatever the cause it doesn’t matter: it’s on you to figure it out and fix it.

The way you figure out and fix a bug is by debugging.

That means it is time to begin your first debugging adventure!

By debugging you can find out exactly what your program is doing so you can fix it.

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

1 Like

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.

2 Likes