How to Add a Loot Item to the Inventory.
Objective: How to Add a LootItem to the Inventory.
Create a new Script called InventoryManager, then add it to the new game Object in the scene rename it as the script name.
We want this script to be able to access it anywhere; to do this, we need to create a static variable of InventoryManager and Cache it to the Awake method.
Now, create an Enum called AllItems, add a list of its members as KeyCardA to KeyCardD.
Create a List of type All Items called _inventoryItems.
Create a Public method called AddItem with a parameter of AllItems, then check if we dont have Items in the _inventoryItems.Add the Item to the List.
Now, create a public method called Remove Item with a parameter of AllItems. This time, check if we have an item in the _inventoryItems. Remove the Item in the List.
Note: We will only remove this Item when we use it to open the door, etc.
Create another script called LootItem and attach the script to the game Object we instantiate from the chest .
Note: At this time, I will set a key card and add the necessary component to work as a cube comes out from the chest.
Open the Loot Item, create an AllItems variable called _itemType.
Now we need to implement the Interface in this script, then go to the Interact method.First thing we need to add _itemType to the InventoryManager, but it's very important to use Instance to make sure we add this itemType to the inventoryManager from the scene.
Last, we destroy this object to give the illusion to the player that we have collected it.
Note: We didn't destroy this item totally since we added this to the inventory manager; we will have its information there if we want to use it to do something else.
See you at the next one.
Note: The way we code this system, even if we collect 10 same items, our inventory will add only one item.
