Null Reference Exception: Object Reference Not Set to an Instance of An Object Error

Hello all,

I’m having issues with having my character pick up items due to the Null Reference Exception: Object Reference Not Set to an Instance of an Object Error.

I’m following the tutorial at the following link below:

At point 25:14 in the video, when we create the HideAll() method, then we test run the game, that is when I get the Null Reference Exception Error. Can someone explain why I’m getting this error?

Here is the code for my Inventory Script, where the HideAll() Function is located. Unity is saying that the Null Reference exception is in this Function, however, my function’s format is exactly the same as the Tutorial video. So why am I getting this error?

public class InventorySystem : MonoBehaviour
{
    [Header("General Fields")]
    // List of items picked up
    public List<GameObject> items = new List<GameObject>();
    //flag indicates if the inventory is open/not
    public bool isOpen;
    [Header("UI Items Section")]
    // UI Reference element - Inventory sys. window
    public GameObject ui_Window;
    public Image[] items_images; // PLEASE DO NOT FORGET THAT THIS IS AN ARRAY!!!!

    [Header("UI Item Description")]
    public GameObject ui_Description_Window;
    public Image description_Image;
    public Text description_Title;
    public Text description_Text;

    private void Update()
    {
        if(Input.GetKeyDown(KeyCode.I))
        {
            ToggleInventory();
        }
    }

    void ToggleInventory()
    {
        isOpen = !isOpen;
        ui_Window.SetActive(isOpen);

    }

    public void PickUp(GameObject item)
    {
        items.Add(item);
        Update_UI(); // update ui as we get items
    }

    void Update_UI()
    {

        HideAll();
        // for each item in the items list
        // Show it in the respective slot in the 'items_images'


        for (int i=0; i < items.Count; i++)
        {
            items_images[i].sprite = items[i].GetComponent<SpriteRenderer>().sprite;
            items_images[i].gameObject.SetActive(true);
        }

    }

    void HideAll() { foreach (var i in items_images) { i.gameObject.SetActive(false); } }
}

Here is the video of the issue I’m experiencing. Basically, when I try to pick up an item, it throws that Null Exception Error as seen.

Any help is appreciated! I cannot move forward into the tutorial until this null exception error has been resolved!

Thank you!

There is only ONE way to move forward with null ref, and posting in the forum will not be that way.

The answer is always the same… ALWAYS. It is the single most common error ever.

Don’t waste your life spinning around and round on this error. Instead, learn how to fix it fast… it’s EASY!!

Some notes on how to fix a NullReferenceException error in Unity3D

  • also known as: Unassigned Reference Exception
  • also known as: Missing Reference Exception
  • also known as: Object reference not set to an instance of an object

http://plbm.com/?p=221

The basic steps outlined above are:

  • Identify what is null
  • Identify why it is null
  • Fix that.

Expect to see this error a LOT. It’s easily the most common thing to do when working. Learn how to fix it rapidly. It’s easy. See the above link for more tips.

You need to figure out HOW that variable is supposed to get its initial value. There are many ways in Unity. In order of likelihood, it might be ONE of the following:

  • drag it in using the inspector
  • code inside this script initializes it
  • some OTHER external code initializes it
  • ? something else?

This is the kind of mindset and thinking process you need to bring to this problem:

https://discussions.unity.com/t/814091/4

Step by step, break it down, find the problem.

Here is a clean analogy of the actual underlying problem of a null reference exception:

https://discussions.unity.com/t/840647/7

Hi all,

I figured out the Null exception Error. The issue was that I recopied and repasted my Items and Images in my Inventory window, causing them to delete from my item_images array. I had to re-add my images from each of my items (as seen on the screenshot below) to my item_images array listed in my Fox’s InventorySystem inspector.

Hope this helps others who are experiencing similar problems!