GameObject transfer information

I don’t know why but I get a null reference exception every time i call this function:

function AddToInventory () {

   var inventory = player.GetComponent(Inventory);
      
   isTrigger = true;
      
   inventory.AddItem(this);
   
   transform.position = inventory.transform.position;

}

I am sending the information here, to this function:

function AddItem (item : GameObject) {

   icon = item.GetComponent(Texture);
   
   items.Add(item);

}

it tells me that the object i’m sending is not set to an instance of an object.

what am I doing wrong here?

Dollars to doughnuts you need to wrap everything after GetComponent in a != null test.

function AddToInventory () {

   var inventory = player.GetComponent(Inventory); // probably doesn't return because you don't actually have the component.

   if (inventory != null) {
     isTrigger = true;

     inventory.AddItem(this);

     transform.position = inventory.transform.position;
   }
}