How to return itemIndex of array

Hello, how to return itemIndex from array, code below doesnt work

    public int GetIndex(int itemIndex)
    {
        return inventorySlots[itemIndex];

    }

Umm… does your monitor blink or the fans on the GPU rev up?

I’m trying to be a bit funny but what do you imagine “doesn’t work” means because we (I’ll bet) don’t have a clue.

1 Like

Im not sure what you need here. Its either a misunderstanding of arrays and indexes, or a language barrier causing misunderstanding of your problem.

You pass itemIndex into the function, you have to know it when you call it. If the index is larger than the array, it will throw an out of range error (you should include a length check before returning).

Have you tried with

?

Im using this function now

    public void RemoveAt(int slotIndex)
    {
        if (slotIndex < 0 || slotIndex > inventorySlots.Length - 1)
        {
            return;
        }
        InventorySlot slot = inventorySlots[slotIndex];
        InventoryItem itemSlot = slot.GetComponentInChildren<InventoryItem>();
        if (itemSlot!= null)
        {
            Destroy(itemSlot.gameObject);
        }

    }
1 Like