I am trying to compare the items in my player inventory to see if any match the objective items on my active quests list. Basically, I need to compare the items in two lists, but not in order (since the player will obviously be filling the quests list and the inventory lists in random order).
I have tried a for
loop and foreach
loop, but in the for
loop, I think the comparison is just to the matching index in each list in order (so this only works if the player gets the items along with the quests in perfect order), and in the foreach
loop, it is only searching the first index, because the first list is populated by a GameObject
and I can’t figure out how to make this work without naming the exact index in activeQuests
(I can’t use “i”).
For the for
loop, is there a way to just compare two lists to see if the elements of one match ANY element of the other (so not in order?) Code here:
screenshot from a text editor|690x199
if (inventory != null && activeQuests.Count != 0)
{
for (int i = 0; i < inventory.Container.Count; i++)
{
if (inventory.Container[i].item.itemName.Contains(activeQuests[i].questObjectName))
{
Debug.Log("Working");
activeQuests[i].questObjectiveMet = true;
}
}
And/or for the foreach
loop, is there a way to search the entire activeQuests
list, instead of just the first element? Code here:
screenshot from a text editor|690x234
if (inventory != null && activeQuests.Count != 0)
{
foreach (InventorySlot i in inventory.Container)
{
if (i.item.itemName.Contains(activeQuests[0].questObjectName))
{
Debug.Log("Working");
activeQuests[0].questObjectiveMet = true;
}
}
}