Created
April 23, 2025 10:23
-
-
Save Mike-Schvedov/b351fef48abd84900449cb6fba99404d to your computer and use it in GitHub Desktop.
RemoveItem Method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public void RemoveItem(string itemName, int amountToRemove) | |
| { | |
| int remainingAmountToRemove = amountToRemove; | |
| foreach (InventorySlot slot in slotList) | |
| { | |
| if (slot.itemInSlot != null && slot.itemInSlot.thisName == itemName) | |
| { | |
| int amountInSlot = slot.itemInSlot.amountInInventory; | |
| int amountToTakeFromThisSlot = Mathf.Min(remainingAmountToRemove, amountInSlot); | |
| slot.itemInSlot.amountInInventory -= amountToTakeFromThisSlot; | |
| remainingAmountToRemove -= amountToTakeFromThisSlot; | |
| if (slot.itemInSlot.amountInInventory == 0) | |
| { | |
| Destroy(slot.itemInSlot.gameObject); | |
| slot.itemInSlot = null; | |
| } | |
| if (remainingAmountToRemove <= 0) | |
| break; | |
| } | |
| } | |
| if (remainingAmountToRemove > 0) | |
| { | |
| Debug.Log("Item not found or insufficient quantity in inventory"); | |
| } | |
| ReCalculateList(); | |
| OnInventoryUpdate?.Invoke(); | |
| QuestManager.Instance.RefreshTrackerList(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment