Inventory, Object reference not set to an instance of an object

Hi, I’m trying to make a functions that allows stacking inside my slotList.
Whenever I try to pickup an object I get this error:
6628285--755581--upload_2020-12-16_16-24-27.png
This is the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Inventory : MonoBehaviour
{
    public GameObject inventoryPrefab;
    public GameObject inventoryUI;

    public List<InventorySlot> slotList = new List<InventorySlot>();

    void Start()
    {

    }

    void Update()
    { 
       
    }

    public void AddToInventory(Item _item, int _amount)
    {
        for (int i = 0; i < slotList.Count; i++)
        {
            if (_item.isStackable && _item.id == slotList[i].item.id)
            {
                slotList[i].AddAmount(_amount);
                return;
            }
            else if(_item.id != slotList[i].item.id)
            {
                //print("false");
            }
        }
        slotList.Add(new InventorySlot(_item, _amount));
    }

    //public void CreateDisplay()
    //{
    //    for (int i = 0; i < slotList.Count; i++)
    //    {
    //        var obj = Instantiate(inventoryPrefab, inventoryUI.transform);
    //    }
    //}

    //public void UpdateDisplay()
    //{

    //}
}

and this is the InventorySlot script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class InventorySlot : MonoBehaviour
{
    public Item item;
    public int amount;

    public InventorySlot(Item _item, int _amount)
    {
        item = _item;
        amount = _amount;
    }

    public void AddAmount(int Value)
    {
        amount += Value;
    }
}

and this is the interaction script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class InteractRay : MonoBehaviour
{
    public Camera playerCam;
    public float rayRange = 2f;

    public Inventory inventory;

    void Start()
    {
       
    }

    void Update()
    {
        InteractionRay();
    }

    public void InteractionRay()
    {
        RaycastHit hit;

        //Debug.DrawRay(playerCam.transform.position, playerCam.transform.forward * 2, Color.red, 0.5f);
        if (Physics.Raycast(playerCam.transform.position, playerCam.transform.forward, out hit, rayRange))
        {
            //print(hit.transform.name);
            if (hit.transform.tag == "Item")
            {
                if (Input.GetKeyDown(KeyCode.F))
                {
                    if (hit.transform.GetComponent<GroundItem>().item)
                    {
                        inventory.AddToInventory(hit.transform.GetComponent<GroundItem>().item, hit.transform.GetComponent<GroundItem>().amount);
                    }
                    //GetComponent<Inventory>().AddToInventory(hit.transform.GetComponent<GroundItem>().item);
                    Destroy(hit.transform.gameObject);
                }
            }
        }
    }
}

the error happens on the following line: inventory.AddToInventory(hit.transform.GetComponent().item, hit.transform.GetComponent().amount);

I don’t know how to fix this.

It means something is null and it is indeed your fault. Separate your GetComponent chain into variables and test each one to identify what is null.

For example.

if (hit.transform.GetComponent<GroundItem>().item)
{
     var groundItem = hit.transform.GetComponent<GroundItem>();
     Debug.Log(groundItem, this);
     Debug.Log(groundItem.item, this);
 
     inventory.AddToInventory(groundItem.item, groundItem.amount);
}

Now you will get a null output in the console and know what is null. Then you can simply use powers of deduction to find out why it is null and how you can prevent it from happening.

There’s only really two items in question here, so it should be easy to follow this back.