Container.findItemOfType C# (CSharp) Method

findItemOfType() public method

Finds the first item of a particular type in the container.
public findItemOfType ( int itemid ) : string
itemid int Itemid.
return string
    public string findItemOfType(int itemid)
    {
        for (int i =0; i<=MaxCapacity(); i++ )
        {
            GameObject obj = GetGameObjectAt(i);
            if (obj!=null)
            {
                if (obj.GetComponent<ObjectInteraction>()!=null)
                {
                    if (obj.GetComponent<ObjectInteraction>().item_id==itemid)
                    {
                        return obj.name;
                    }
                    else
                    {
                        if (obj.GetComponent<ObjectInteraction>().GetItemType()==ObjectInteraction.CONTAINER)
                        {
                            string ans= obj.GetComponent<Container>().findItemOfType(itemid);
                            if (ans!="")
                            {
                                return ans;
                            }
                        }
                    }
                }
            }
        }
        return "";
    }

Usage Example

    public ObjectInteraction findObjInteractionByID(int item_id)
    {    //Returns the first instance of a particular Item id in the players inventory
        for (int i = 0; i <= 10; i++)
        {
            GameObject obj = GetGameObjectAtSlot(i);
            if (obj != null)
            {
                ObjectInteraction objInt = obj.GetComponent <ObjectInteraction>();
                if (objInt != null)
                {
                    if (objInt.item_id == item_id)
                    {
                        return(objInt);
                    }
                }
            }
        }
        //playerUW.GetComponent<Container>();
        string find = playerContainer.findItemOfType(item_id);

        if (find != "")
        {
            GameObject        obj    = GameObject.Find(find);
            ObjectInteraction objInt = obj.GetComponent <ObjectInteraction>();
            if (objInt != null)
            {
                if (objInt.item_id == item_id)
                {
                    return(objInt);
                }
            }
        }
        return(null);
    }
All Usage Examples Of Container::findItemOfType