How to find IndexOf of type?

I am trying to do an inventory where if inventory is = 0, i simply add to the inventory list, and if its not equal to 0, it inserts to the next index.

public GameObject player;
public InventoryItem thisItem;

   

PlayerInventory pInv;

void Start()
{
    pInv = player.GetComponent<PlayerInventory>();
}

public void AddToInventory()
{
    if (pInv.inventory.Count == 0 && pInv.inventory.Count != 6)
    {
        pInv.inventory.Add(thisItem);
        Debug.Log(thisItem.name);

        Destroy(gameObject);
    }
    else if (pInv.inventory.Count != 0 && pInv.inventory.Count != 6)
    {

        int itemIndex = pInv.inventory.IndexOf(thisItem);

        pInv.inventory.Insert(itemIndex, thisItem);
        Debug.Log(itemIndex);
        Debug.Log(thisItem.name);

        Destroy(gameObject);
    }
    else if (pInv.inventory.Count == 6)
    {
        Debug.Log("Inventory is Full!");
    }
}

The problem with this is that thisItem is of InventoryItem which is a scriptable object i made, for testing purposes i made 2, key and journal. I attatched key to three objects and journal to four objects to simply try maximum inventory. The problem rose when i tried to get an object of the other item, because thisItem is referenced by a specific inventory item, the code only detects the index of that specific inventory item and not the type of inventory item. Any help?

If you want to find the index of something that matches a certain criteria, use List<T>.FindIndex instead.

Probably something like int index = pInv.inventory.FindIndex(x => x.GetType() == thisItem.GetType()); if I understand your question (it’s hard to tell what you’re actually asking).

my apologies if its hard to understand cus its difficult to explain, i made a scriptable object called InventoryItem, and in my inventory pickup script, i referenced the object being picked up to a certain inventory item (for ui purposes, etc). and to pick it up and add to my inventory (a list, not array) i am adding the specific item itself. but the problems starts when i try to get more items to my inventory of a different InventoryItem, but since i use indexof to detect the specific item and not the inventoryitem class to find the next available spot, when i pick up a book, i cant pick up the key and vice versa. also what is the x supposed to be?

What is the script in your original post attached to? It looks like it destroys itself after an item is added. How does “public InventoryItem thisItem” get set?

Are you trying to add items to the inventory such that items of the same category get grouped together? Are the key and book different classes, or just different instances of a common scriptable object?

the script is attatched to the item in the scene, this item gets assigned through the inspector as a reference to the actual inventory item. and no im not trying to group together but now i realize thats not really a problem since every item is going to be unique anyway. and they are indeed different instances of the common scriptable object

I mean based on ‘style’ of inventory (max six items), your use IndexOf of is unnecessary? If there’s less than 6 items… just add it to the list. I don’t think it needs to be more complicated than that?

i could do that, but i want it so i can drop the object, remove it from list (without affecting other elements if thats possible) and insert the item in the free element slot.

You can still do all this though? If the inventory has less than six items, there is no need for any insertion. Just add it at the end. Then you can still remove items from whatever index.

yeah but the ui is my biggest problem, do i also have to move the ui slots backward or something? or is there another work around for this, apologies if i ask a lot of questions.

Nah your questions are completely valid.

UI should often just ‘present’ the state of some underlying data (separation of concerns). So how you manage the players inventory should be separate to how its presented via UI. Ideally the UI takes care of itself, and probably just listens to changes in the inventory (probably via delegates).

I don’t know how your UI is meant to look, so hard to give any concrete suggestions.

thanks for the advice! ill figure something out then ill come back if i have any more questions, thank uuu!

1 Like

Well, this script is most likely attached to some kind of collectible item in the world. Though I do get your confusion as this kind of logic should be part of the inventory and not an item or external script. So the overall approach here is kinda wrong as this script gets the inventory of the player and adds the item that is assign to itself to the inventory. The logic how items should be added or removed should be part of the inventory itself. So usually you would have a method “AddItem” or something like that on the inventory script and it does the actual “adding”, stacking and checking.

@Janskie
As I just said, you may want to rethink some of your design decisions. To us it’s still not clear what pInv.inventory actually is. It seems to be a generic List of InventoryItems and it seams you only have / want 6 slots? Since you store individual items, you don’t plan to have items stacked, do you?. So you can only store individual items in the inventory. Why is it important in which empty slot the item gets moved? When you use a list, you usually never have empty slots. If you wish to have a fixed sized inventory, you usually would just use an array and empty slots are just null. In a list you would simply add items at the end. Maybe your intention is to “group” the same items next to each other? So when you have A B C in the inventory and you collect another “B” you end up with A B B C instead of A B C B?

Most inventory systems that allow items to “stack” actually store ItemStacks in inventory slots which are just thin wrappers around the actual item and add some metadata like the amount / count. Though this all depends on what kind of inventory you want and how it’s actually implemented at the moment. We don’t know very much about it yet.

1 Like

sincere apologies for the conclusion. i attatched the add to inventory script on the collectible item itself because i have an interact system on the items, a custom on interact event, also because i dont know how to do inventory logic? im not saying im new to coding but still fairly unknowledgable. and yes i do not plan to have items stacked. ill try to use an array for the inventory instead, ill try to give updates. thank you for your responses. if you need more info i can gladly send more. Just to be clear i want my inventory to be a survival horror style, not drag and drop like rpg.