Search Unity

Hello. I have a problem with a list.

Discussion in 'Editor & General Support' started by SilentParrotStudio, Sep 17, 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! :)