Search Unity

Search for objects in a list..

Discussion in 'Scripting' started by Bydark, Apr 19, 2017.

  1. Bydark

    Bydark

    Joined:
    Oct 9, 2013
    Posts:
    10
    Hello everyone, I am trying to create an inventory for my game, and I would like to find out how to know if an object is inside a list, my script is like this:


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Inventory {
    6.  
    7.     public string name;
    8.     public int health;
    9.  
    10.     public Inventory(string newName, int newHealth)    {
    11.         name = newName;
    12.         health = newHealth;
    13.     }
    14. }
    15.  



    To add an object to my list I do the following:


    Code (CSharp):
    1. List<Inventory> inventario = new List<Inventory> ();
    2.         inventario.Add (new Inventory ("Apple", 1));


    What I want to do is to know if my list exists on the apple or any other object.

    I hope you can help me, thank you very much.

    PS: I'm sorry for my English D:
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    List.Contains will work for simple cases. For a more sophisticated approach you can use a for loop and iterate accross the entire list.
     
    SnaveMot and Bydark like this.
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    I think List.Contains will not work in this case - because Inventory is a class it'll compare reference values, and won't find it unless you do inventario.Contains(theExactInstanceYouAreCheckingFor)... which is probably not practical.

    So to expand on the for loop:
    Code (csharp):
    1. for (int i=0; i< inventario.Count; i++) {
    2. if (inventario[i].name == "Apple") {
    3. // Yes
    4. break; //don't need to check the remaining ones now that we found one
    5. }
    6. }
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Well, a simple way is to just loop through your inventory. There isn't anything wrong with using a loop.
    You could also use something like the following

    Code (CSharp):
    1. Inventory inventoryItem = inventario.Find((x) => x.name == someString);
    inventoryItem would be null if it doesn't find anything or it will be the item in the list if it does find it, if you're trying to interact with it somehow. (check it's count, subtract from it, add to it, etc).
     
    shekalo, weto4ka, W1zzel and 2 others like this.
  5. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    This isn't quite correct. Contains uses the Equals implementation. So if you've overridden Equals, it'll return true if you're looking for a different object that is equal according to that method.

    Of course, you're otherwise correct in that it's not a good solution here. You'll probably not want to create a new inventory item just to check if a different one exists, even if there's an Equals implementation.


    If you just need to know if something named "apple" is in the list, and don't need to do anything with it (for whatever reason), the built-in thing to do that is Linq's Any:

    Code (csharp):
    1. bool hasApple = inventario.Any(x => x.name == "Apple");
     
  6. Bydark

    Bydark

    Joined:
    Oct 9, 2013
    Posts:
    10
    Thanks to everyone for the help, in the end I ended up doing it this way.

    Code (CSharp):
    1. void Buscar (string nombre)
    2.     {
    3.  
    4.  
    5.  
    6.  
    7.         Inventory item = inventario.Find(x=> x.name == nombre);
    8.  
    9.         if(item == null){
    10.             Debug.Log ("No hay manzanas");
    11.         }
    12.         else{
    13.             Debug.Log ("Si hay manzanas");
    14.         }
    15.  
    16.  
    17.  
    18.  
    19.        
    20.     }
     
  7. Arcaane

    Arcaane

    Joined:
    Feb 16, 2021
    Posts:
    1
    Gracias desde la francia! me has salvado la vida
     
    orestecolatruglio likes this.