public void TakeItem() { Item item = pickupUI.currentlySelectedItem; if (!item) { return; } // Looking for items to stack with List <Item> search = items.FindAll((Item x) => x.ID == item.ID); int currentQuantity = item.quantity; foreach (Item x in search) { int cap = x.quantity + currentQuantity; if (cap <= x.maxStack) { currentQuantity = 0; inventoryUI.ChangeItemQuantity(x, cap); pickUpController.RemoveItemFromRange(item); Destroy(item.gameObject); break; } else { currentQuantity = cap - x.maxStack; if (x.quantity != x.maxStack) { inventoryUI.ChangeItemQuantity(x, x.maxStack); } } } // If currentQuantity != 0 we need to add new element only if there is space if (currentQuantity != 0) { if (currentCapacity >= maxCapacity) { if (currentQuantity != item.quantity) { pickupUI.ChangeItemQuantity(item, currentQuantity); } } else { currentCapacity++; inventoryUI.ChangeCurrentCapacity(currentCapacity); inventoryUI.AddItemToInventory(item); items.Add(item); pickUpController.RemoveItemFromRange(item); item.gameObject.SetActive(false); if (pickupUI.GetItemsCount() <= 0) { pickupUI.ToggleInventoryGUI(); } } } }
void OnTriggerEnter(Collider collider) { var storable = collider.GetComponentInParent <Storable>(); if (storable != null) { if (InventoryUI.AddItemToInventory(storable)) { AudioSource.Play(); storable.gameObject.SetActive(false); } } }