Passing gameObjects to Another Script: NullReference Exception

I’m having difficulty passing gameObjects from one script to another.

I have a function called addItems(1-object-here) to add to an arraylist in Inventory.cs

I have a function called pickUpItem() to called on addItems in PickupItem.cs

It is giving me an error that there is a nullreference exception
I’m trying to follow the Unity documents for this, and I have it nearly exactly the same.

In PickUpItems.cs:

// Update is called once per frame
	void Update () {
	PlayerNearby();
	
	if(Input.GetKeyDown("space"))
    {
     pickupItem();
     }
	}
	
	void pickupItem()
	{
			if (nearby)
	{
		Debug.Log (nearby);
		GameObject.Find("player").GetComponent().addItem(this.gameObject);
		Destroy (this);
	}
	}

In Inventory.cs:

public class Inventory : MonoBehaviour {
	 public ArrayList inventoryL = new ArrayList();


	// Use this for initialization
	void Start () {
		
	}
	
	public void addItem(GameObject item)
	{
		inventoryL.Add(item);
	}
	
	public void removeItem(GameObject item)
	{
		for(int i = 0; i < inventoryL.Count; i++)
		{
			if(inventoryL *== item)*
  •  	{*
    
  •  		inventoryL.RemoveAt(i);*
    
  •  	}*
    
  •  }*
    
  • }*

  • // Update is called once per frame*

  • void Update () {*

  • }*

Unless c++ works different as javascript your not telling what component to acces:

GameObject.Find("player").GetComponent().addItem(this.gameObject);

I think you want that to be

GameObject.Find("player").GetComponent(Inventory).addItem(this.gameObject);

Also you are destroying the pickup so I am not sure what kind of effect that gives on the List, perhaps a null refence as the item where the list entry links to is destroyed.
What makes me ask why you need to store something in a list if you destroy it right after.

The way pikups work in my game is by telling my player what item is unlocked and add that to a list or array.

In case you want to prepare for possible multiplay you can consider hiding the pickup instead of destroying and set a reset timer to make it pop again.