Weird Inventory Issue

I have a script for a simple 40 slot inventory. My issue has to do with item placement in the inventory. If I pick up an item in game without opening the inventory first, the item goes into the 10th slot. But, if I open the inventory before picking up the item, it goes into slot 1. Can anyone help look over the code. I’ve gone through it so long it getting blurred to me. I’m sure it’s something stupid, I just can’t find it.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityStandardAssets.Characters.FirstPerson;

public class Inventory : MonoBehaviour
{

    //Variables
    public bool inventoryEnabled;
    public GameObject inventory;
    public GameObject itemDatabase;
    private Transform[] slot;
    public GameObject slotHolder;
    public GameObject message;
    private bool collision = false;
    private string collisionTag;
    private GameObject itemName;

    //Functions
    public void Start()
    {
        inventory.SetActive(true);
        GetAllSlots();
     
    }

    public void Update()
    {
        //Enabling and disabling the inventory
        if (Input.GetKeyDown(KeyCode.I))
            inventoryEnabled = !inventoryEnabled;

        if (inventoryEnabled)
        {
            inventory.SetActive(true);
            GetComponent<FirstPersonController>().enabled = false;
            Cursor.visible = true;
            Cursor.lockState = CursorLockMode.None;
        }
        else
        {
            inventory.SetActive(false);
            GetComponent<FirstPersonController>().enabled = true;
            Cursor.visible = false;
            Cursor.lockState = CursorLockMode.Confined;
        }

        if(inventoryEnabled == true)
        {
            if (Input.GetKeyDown(KeyCode.Escape))
            {
                inventory.SetActive(false);
                inventoryEnabled = !inventoryEnabled;
                GetComponent<FirstPersonController>().enabled = true;
                Cursor.visible = false;
                Cursor.lockState = CursorLockMode.Confined;
            }
        }

     
        if(collision == true && Input.GetKeyDown(KeyCode.F))
        {
            PressedToPickUp();
        }
    }

    public void OnTriggerEnter(Collider other)
    {
        itemName = other.gameObject;
        collisionTag = other.tag;
        collision = true;
        message.SetActive(true);
    }

    public void OnTriggerExit()
    {
        message.SetActive(false);
    }

    public void PressedToPickUp()
    {
        if (collisionTag == "Item")
        {
            AddItem(itemName.gameObject);
        }
    }

    public void AddItem(GameObject item)
    {
        GameObject rootItem;
        rootItem = item.GetComponent<ItemPickup>().rootItem;

        for (int i = 0; i < 40; i++)
        {
            print(slot);
            if (slot.GetComponent<Slot>().empty == true && item.GetComponent<ItemPickup>().pickedUp == false)
            {
                slot.GetComponent<Slot>().item = rootItem;
                item.GetComponent<ItemPickup>().pickedUp = true;
                message.SetActive(false);
                Destroy(item);
                i = 41;
            }
        }
    }

    public void GetAllSlots()
    {
        slot = new Transform[40];
        for (int i = 0; i < 40; i++)
        {
            slot = slotHolder.transform.GetChild(i);
        } 
    }
}

Your post is unreadable, please edit it and put your code into code tags.

Sorry about that. First time posting to the forums

Replace this with break;

The GetComponent() is not reliable way to tell where to put what. Get all slots once in an array and iterate over that to determinate if you have an empty slot.

I don’t understand what you mean when you say “iterate over that to determinate if you have an empty slot”. I’ve only been coding for a short time. Could you show an example of what you mean?

have an array of Slot[ ] and use that instead of a Transform[ ] array called slots.

never mind the bug, this is just better for everything.