Change value in array.

I am qualified game dev so I should probably know this but I am stumped please help;

void OnTriggerEnter(Collider other){
		if (other.gameObject.tag == "Player") {
			if (GetComponent<Inventory>().slotNum[0] == 0){
				GetComponent<Inventory>().slotNum[0] = ItemID;
			else if (GetComponent<Inventory>().slotNum[1] == 0){
				GetComponent<Inventory>().slotNum[1] = ItemID;
			else if (GetComponent<Inventory>().slotNum[2] == 0){
				GetComponent<Inventory>().slotNum[2] = ItemID;


When you use GetComponent with out say a variable where you set or found a gameObject(go.GetComponent), you will be looking for that component or script on the object that has the script that is invoking that command. The code in the original post above assumes that Inventory exists on the same GameObject as the script above with the OnTriggerEnter method being invoked and is directly “gettable” and not a child of the parent or child of a child of a child of a child ad Infinitum et ultra.

If the inventory script is on the cam, then you can get a reference to the main camera super easy.

Camera cam = Camera.main;

Inventory inv = cam.GetComponent<Inventory>();

if (inv.slotNum[0] == 0){
  inv.slotNum[0] = ItemID;
else if (inv.slotNum[1] == 0){
  inv.slotNum[1] = ItemID;
else if (inv.slotNum[2] == 0){
  inv.slotNum[2] = ItemID;