Inventory system - pickup items

Hi!

I have this basic inventory set up. But i dont how can i implement system for pick up items. Basically you click on an item in the game world and then that item gets destroyed or its render will be set to visible(false) and in the inventory icon will appear that represents that particular item.

Inventory script:

    void start ()
    {

    }

    void Update ()
    {
       ToogleInventorywindow ();
    }

    void OnGUI ()
    {
        if (visible)
        {
            if (_displayInventoryWindow)
                _inventoryWindowRect = GUI.Window(INVENTORY_WINDOW_ID, _inventoryWindowRect, InventoryWindow, "Inventory");
        }
    }

    public void InventoryWindow (int id)
    {
        for (int y = 0; y < _inventoryRows; y++)
        {
            for (int x = 0; x < _inventoryCols; x++)
            {
                GUI.Label(new Rect(5 + (x * buttonWidth), 20 + (y * buttonHeight), buttonWidth, buttonHeight), (x + y * _inventoryCols).ToString(), "box");
            }
        }
            GUI.DragWindow();
    }

You can use OnMouseDown

You should set up a data model that is shared by both the gui and the pickup scripts. Here is a very hackish example.

[Serializable]
    public class Item {

     public static readonly List<Item> MyInventory = new List<Item>();

     public string Name;
    }

[SerializeField]
public Item Info;
void OnMouseDown(){
 Destroy(gameObject);
 Items.MyInventory.Add(Info);

}

void OnGui(){
foreach(var item in Item.MyInventory){
DrawItem(item);
}

You can use simple variables or a specific structure (class) to represent each item’s quantity, like in this question.

Destroying the picked item is easier - an invisible item (renderer disabled) still can produce trigger events, and can be hit by raycasts (I had such problem once: the raycast shots hit the item triggers, saving the player and producing weird sparks in the air!).