How to add item to my inventory when I pick them up?

I’m somewhat new to Unity and game development but I keep getting this error:

NullReferenceException: Object reference not set to an instance of an object
HUD.InventoryScript_ItemAdded (System.Object sender, .InventoryEventArgs e) (at Assets/HUD.cs:21)
inventory.AddItem (IInventoryItem item) (at Assets/Scripts/inventory.cs:42)
collider.OnCollisionEnter (UnityEngine.Collision col) (at Assets/Scripts/collider.cs:38)

Usually when I get a NullReferenceException, the fixes is dragging in the object that a script is referring to. From what I can see here, the collider (which is on the player) is detecting the collision between the player and the item. It sent the information of collision object to the inventory class which adds the object to the List of inventory items. From here things get a bit murky as it throws the error in the HUD.InventoryScript_ItemAdded method.

Here is the HUD.cs script

using UnityEngine;
using UnityEngine.UI;

public class HUD : MonoBehaviour {

    public inventory Inventory;

    // Use this for initialization
    void Start () {

        Inventory.ItemAdded += InventoryScript_ItemAdded;
	
    }

    private void InventoryScript_ItemAdded(object sender, InventoryEventArgs e)
    {
        Transform inventoryPanel = transform.Find("InventoryPanel");
        foreach(Transform slot in inventoryPanel)
        {

            //Border to image
            Image image = slot.GetChild(0).GetChild(0).GetComponent<Image>();

            //We found the empty slot
            if (!image.enabled)
            {
                image.enabled = true;
                image.sprite = e.Item.Image;

                //TODO: Store reference to the item

                break; //what does this do?
        }
    }
 }




// Update is called once per frame
void Update () {
	
}
}  

This is the inventory script

public class inventory : MonoBehaviour {

//fixed number of slots
private const int SLOTS = 10;

//creates an arraylist of inventory items
private List<IInventoryItem> mItems = new List<IInventoryItem>();

//handles event?
public event EventHandler<InventoryEventArgs> ItemAdded;

//adding item method
public void AddItem(IInventoryItem item)
{

    //if there are free slots available
    if (mItems.Count < SLOTS)
    {

        //collide with item
        Collider collider = (item as MonoBehaviour).GetComponent<Collider>();

        
        if (collider.enabled)
        {
            //if the collider of the object is enabled turn it off
            collider.enabled = false;

            //add to array list
            mItems.Add(item);

            item.OnPickup();

            //all subscribers of event are notfified?
            if(ItemAdded != null)
            {
                ItemAdded(this,new InventoryEventArgs(item));
            }
        }
    }
}
}

And finally this is the collider script I put on my player

public class collider : MonoBehaviour
{
    public inventory Inventory;
    //public GameObject pickupEffect;
    public gun g;

    void OnTriggerEnter(Collider other)
{
    
    if(other.gameObject.tag == "item")
    {
        Debug.Log("You Hit an item");
        Destroy(other.gameObject);

    }

    if(other.gameObject.tag == "ammo")
    {
        Debug.Log("Picked up ammo");
        Destroy(other.gameObject);
        g.ammo += 30;
    }
}

void OnCollisionEnter(Collision col)
{
    
    if(col.gameObject.tag == "collectable")
    {
        Debug.Log("This object is collectable");
        IInventoryItem item = col.gameObject.GetComponent<IInventoryItem>();
        if (item != null)
        {
            Inventory.AddItem(item);
            Debug.Log("Collided with an inventory item");
        }
    }

   
}

/*
 * 
//check if item collided with has an inventory item interface
private void OnControllerColliderHit(ControllerColliderHit hit)
{
    Debug.Log("Collided with an inventory item");
    IInventoryItem item = hit.collider.GetComponent<IInventoryItem>();
    if(item != null)
    {
        Inventory.AddItem(item);

        Debug.Log("Collided with an inventory item");
    }
}

*/

}

Sorry if the code is not formatted correctly but the system for creating code in this forum seems to be a bit tedious because you have to space 4 times for each line.

Anyway, why am I getting this error and how could I fix it while implementing this code?

According to your error message “(at Assets/HUD.cs:21)” this seems to be the problem:

Image image = slot.GetChild(0).GetChild(0).GetComponent<Image>();

GetChild could return null if there are no children.
Try to do it in stages to better be able to debug it. Set a breakpoint and step through and check the return values.

var child1 = slot.GetChild(0);
var child2 = child1.GetChild(0);
var image = child2.GetComponent<Image>();

But there’s probably a better way to get access to the Image component. Maybe GetComponentInChildren<Image>() if there is only one Image component in the descendants hierarchy. Or give the GameObject with the Image component on it a name and use

var gameObj = slot.find("ImageContainer");
Image image;
if(gameObj != null) {
  image = gameObj.GetComponent<Image>();
}