How do I trigger an event if something is equal to one or more variables in an array/list?

So I have an inventory system that clears items from an inventory when using them, with the exception of a few key items. The problem is that I have to list every item individually, and you can probably figure this isn’t practical. Is there a way to loop through all the items in the array/list to check whether it’s a key item or not?

Here’s the method:

public void OnSlotSelected(InventorySlot selectedSlot)
{
    currentSelect = selectedSlot;

    if (selectedSlot.itemData == null)
    {
        _itemNameText.ClearMesh();
        _itemDescriptionText.ClearMesh();
        return;
    }

    if (selectedSlot.itemData == keyItems[0] || selectedSlot.itemData == keyItems[1] || selectedSlot.itemData == keyItems[2])
    {
        isKeyItem = true;
    }
    else
    {
        isKeyItem = false;
    }

    _itemNameText.SetText(selectedSlot.itemData.Name);
    _itemDescriptionText.SetText(selectedSlot.itemData.Description[0]);
}

If you make keyItems a List<T> instead of an Array, then you get a function that does exactly what you want:

keyItems.Contains(selectedSlot.itemData)

I believe you can also do the same thing on an Array, if you add using System.Linq; to your using statements at the top of the script.

If you’re not familiar with Linq, it takes a little getting used to, but is absolutely worth learning when you’re ready.

You can use a simple for loop to check everything in the array:

isKeyItem = false;
for(int i=0; i<keyItems.Length; i++)
{
    if(selectedSlot.itemData == keyItems[i])
    {
        isKeyItem = true;
    }
}

But it may be more convenient if you could add a parameter to InventorySlot to mark something as a key item at the time it is placed in the inventory. That way, you don’t have to run this loop as often.