NullReferenceException: Object reference not set to an instance of an object pickUp.Start ()

Hello! I have a error in my 2D game :
NullReferenceException: Object reference not set to an instance of an object
pickUp.Start () (at Assets/Scripts/pickUp.cs:12)
and I’ve tried everything to fix it, i just can’t figure it out please help me!

My script :

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

public class pickUp : MonoBehaviour
{
    private Inventory inventory;
    public GameObject itemButton;

      private void Start()
    {
        inventory = GameObject.FindGameObjectWithTag("Player").GetComponent<Inventory>();
    }

    void OnTriggerEnter2D(Collider2D other)
    {
        if (other.CompareTag("Player"))
        {
            for (int i = 0; i < inventory.slots.Length; i++)
            {
                if(inventory.isFull[i] == false)
                {
                    //can be added to inventory
                    inventory.isFull[i] = true;
                    Instantiate(itemButton, inventory.slots[i].transform, false);
                    Destroy(gameObject);
                    break;
                }
            }
        }

    }
}

NullReferenceErrors are some of the easiest to solve. They simply mean something has no value. In this case, you are probably missing something tagged as “Player” or it can’t find your “Player” since it’s occuring on line 12.

Thank you! As you said it was very easy to solve just had to add a tag to my player. :slight_smile: