Search Unity

(HELP) how to see if list contains certain scriptable objects

Discussion in 'Scripting' started by GabbyBoy, Sep 23, 2022.

  1. GabbyBoy

    GabbyBoy

    Joined:
    Mar 16, 2020
    Posts:
    4
    followed this bmo tutuorial but I'm doing an adventure game and want to do an inventory check for an item (for example, you can't go through a certain door unless you have a certain key, etc.) but I've tried list.contains over and over and I can't figure it out.

    Anybody know a good way to check inventory list for certain items? (note: I can't just type if(inventory.Contains("Red Key") or something because I get a "Can't convert type inventory item to type string" error.)

    Here's the tutorial in case anyone wants more info, this is basically exactly what I did.


    Thanks in advance!
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,930
    List<T>.Contains
    wants a value that matches the type of the list.

    So in the case of a locked door, give the door component a reference to the SO you want to check for (by exposing it in the inspector and dropping in the right SO), then you can use that reference in List<T>.Contains.
     
    GabbyBoy likes this.
  3. GabbyBoy

    GabbyBoy

    Joined:
    Mar 16, 2020
    Posts:
    4
    You solved my problem spiney, thank you! For anybody else who may stumble upon this in the future-- I had to reference the itemdictionary, not the inventory list. I think it was because the itemdictionary is what holds the itemdata scriptable object stuff. Not sure, this is all a little over my head but it works now to so there you go! Thanks again spiney!
     
    spiney199 likes this.