Inventory bug -

Hey all,

I’m new to programming and making games and learning as I go.

Currently stuck on a bug with inventory and showing items in the inventory when picked up.

I have found the lines causing the issue which are 17, 18 and 26. I have them currently commented out.

If I run the game and pick up the items, they go into my inventory and shows the icon I set and how many I picked up. But I get:

NullReferenceException: Object reference not set to an instance of an object
Slot_UI.SetEmpty () (at Assets/Scripts/Scripts/Slot_UI.cs:26)
Inventory_UI.Setup () (at Assets/Scripts/Scripts/Inventory_UI.cs:53)
Inventory_UI.ToggleInventory () (at Assets/Scripts/Scripts/Inventory_UI.cs:33)
Inventory_UI.Update () (at Assets/Scripts/Scripts/Inventory_UI.cs:23)

If I run the game and open my inventory before picking up anything and close it and pick up something and then open my inventory, it doesn’t show the icon or chnage the value of how many were picked up. I get this error:

NullReferenceException: Object reference not set to an instance of an object
Slot_UI.SetItem (Inventory+Slot slot) (at Assets/Scripts/Scripts/Slot_UI.cs:17)
Inventory_UI.Setup () (at Assets/Scripts/Scripts/Inventory_UI.cs:49)
Inventory_UI.ToggleInventory () (at Assets/Scripts/Scripts/Inventory_UI.cs:33)
Inventory_UI.Update () (at Assets/Scripts/Scripts/Inventory_UI.cs:23)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;

public class Slot_UI : MonoBehaviour
{

    public Image itemIcon;
    public TextMeshProUGUI quantityText;

    public void SetItem(Inventory.Slot slot)
    {
        if(slot != null)
            {
          //  itemIcon.sprite = slot.icon;
          //  itemIcon.color = new Color(1, 1, 1, 1);
            quantityText.text = slot.count.ToString();
        }
    }

    public void SetEmpty()
    {
        itemIcon = null;
      //  itemIcon.color = new Color(1, 1, 1, 0);
        quantityText.text = "";
    }
}

Has anyone worked on sometime like this before and can see the issue?

That’s not necessary.

The answer for this error is ALWAYS the same and it never matters what you are doing, and it also never needs a forum post.

How to fix a NullReferenceException error

https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

Three steps to success:

  • Identify what is null ← any other action taken before this step is WASTED TIME
  • Identify why it is null
  • Fix that

Looks to me like you haven’t assigned an image to your itemIcon field in the inspector?
Also line 26 will always fail because you set your icon to null and after that you are trying to set its color. I suppose you want to do: itemIcon.sprite = null ?

And it is worth checking out Kurt’s link because null pointer will pop up sooo often :slight_smile:
But you were able to identify the lines with problems (they are also mentioned in the error message) so it is easy to see what could be null here which is why I came to my conclusion.