Search Unity

Hello. I have a problem with a list.

Discussion in 'Scripting' started by SilentParrotStudio, Sep 18, 2014.

  1. SilentParrotStudio

    SilentParrotStudio

    Joined:
    Aug 13, 2014
    Posts:
    12
    Hello. I have a problem with a list.

    I have this script:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class StonePickup: MonoBehaviour
    5. {
    6. void OnMouseOver()
    7.     {
    8.             if (Input.GetMouseButtonDown(0))
    9.             {
    10.                 for(int i = 0; i < InventoryP.Inst.Items.Count; i++)
    11.                 {
    12.                         if (InventoryP.Inst.Items[i].Name == "Stone"))
    13.                         {
    14.                             InventoryP.Inst.Items[i].Count +=1;
    15.                         }
    16.                         if (InventoryP.Inst.Items[i].Name != "Stone")
    17.                         {
    18.  
    19.                             Item ITM = new Item();
    20.                             ITM.Name = "Stone";
    21.                             ITM.Count += 1;
    22.                             ITM.ModelForBarTool = StoneH;
    23.                             InventoryP.Inst.Items.Add(ITM);
    24.                             Destroy(gameObject);
    25.                         }
    26.             }
    27.         }
    28.     }
    29.  

    When I pickup the item, I want to add the same item if "Stone" item doesn't exist, and if exist, I want to add only to count, but this script doesn't work. How can I search in a list to find and item by name? I tried with 'InventoryP.Inst.Items.Name == "Stone" ' ,but it doesn't work. Sorry for my English and please, help me! I hope you understand! :)
     
  2. Limeoats

    Limeoats

    Joined:
    Aug 6, 2014
    Posts:
    104
    Hi Cristi97Gaming!

    Your logic is slightly incorrect here. You're not going to want to do the item creation / adding to the list within the for-loop.
    Take a look at the code below. It should work properly.


    Code (CSharp):
    1. void OnMouseOver()
    2. {
    3.     if (Input.GetMouseButtonDown(0))
    4.     {
    5.         foreach (Item item in InventoryP.Inst.Items) //assuming InventoryP.inst.Items is the name of your list
    6.         {
    7.             if (item.Name == "Stone")
    8.             {
    9.                 item.Count += 1;
    10.                 return;
    11.             }
    12.         }
    13.         Item ITM = new Item();
    14.         ITM.Name = "Stone";
    15.         ITM.Count = 1;
    16.         ITM.ModelForBarTool = StoneH;
    17.         InventoryP.Inst.Items.Add(ITM);
    18.         Destroy(gameObject);
    19.     }
    20. }
     
    SilentParrotStudio likes this.
  3. SilentParrotStudio

    SilentParrotStudio

    Joined:
    Aug 13, 2014
    Posts:
    12
    Limeoats, thank you so much! It works now!
     
  4. Limeoats

    Limeoats

    Joined:
    Aug 6, 2014
    Posts:
    104
    Glad to hear it! Good luck and enjoy~
     
    SilentParrotStudio likes this.
  5. SilentParrotStudio

    SilentParrotStudio

    Joined:
    Aug 13, 2014
    Posts:
    12
    Thank you! You too! :)
     
  6. BmxGrilled

    BmxGrilled

    Joined:
    Jan 27, 2014
    Posts:
    239
    You could also do this, and would save a few lines of code! :)
    Code (CSharp):
    1.  
    2.     void OnMouseOver()
    3.     {
    4.         if (Input.GetMouseButtonDown(0))
    5.         {
    6.             //Find the item in the List
    7.             Item item = InventoryP.Inst.Items.Find(itemInList => itemInList.Name == "Stone");
    8.             if (item != null)
    9.             {//null checks are nice, they prevent crashes and help with debugging
    10.                 item.Count += 1;
    11.                 Item ITM = new Item();
    12.                 ITM.Name = "Stone";
    13.                 ITM.Count = 1;
    14.                 ITM.ModelForBarTool = StoneH;
    15.                 InventoryP.Inst.Items.Add(ITM);
    16.                 Destroy(gameObject);
    17.             }
    18.         }
    19.     }
    Lists are lovely things! :) lol
     
  7. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    That is lovely code indeed! Also, if you include "using System.Linq" at the top of your file, you get access to a bunch more great list extension methods that let you do things like find the sum or average of all the items in a list, select all the items that meet some criteria, etc. A good overview of what's available is here.

    (I'm sure you already knew this, Emma, but thought I'd point it out for others... this is one of those gems every C# coder should have in their toolbox!)
     
    BmxGrilled likes this.