moving a object from ground to inventory

Hi I made a inventory with 28 slots and each slot has box collider named “inventory slot1” and it goes on like that tell it hits the 28th.

I need it so when i pick up a item it goes to 1st empty slot in my inventory and have option to drop the item from the inventory to be in front of the character.

If you can include a pick-up script it help out a lot.

thankyou

OK, an inventory is simply a collection of items. These items are of some data type, for simplicity thing of these as integers.

0 - Sword
1 - Bow
2 - Health_Potion

Assign these numbers to an object. How? Make a script that describes the objects behavior to act like an item. The script called “Item”. Now, how can we describe what the behavior of the script is or what it even is? Let’s assign it the numbers we defined previously.

So, this object “Cube” has now have a script “Item” attached to it, given the number 0, meaning this item is our “Sword”.

Ok, we have no defined our items. Now, we need some way to store this in our inventory. Simply this can be done by a list. This list is meant to store our items, so it makes sense the list is of data type “Items”. List inventory = new List

Now then we have our inventory created. How about adding stuff. Ok, when do we want to do this? Lets say we do this when the player presses the ‘F’ key and is within 1meter of the object.

Now then. We need to know how to get our items in the first place that is in the scene.
Let’s cache all our items. AllItems[] = GameObject.FindGameObjectsWithTag()
This will store all of our “Items” type object into an array.

So, we need to traverse through this array and check if the object is within distance of our player AND if the player so happens to press the ‘F’ key we pick up the object.
Simply this can be done with inventory.add(item) Note this item is different from our “Items” type.
Now then at that point you have 2 choices. You can either destroy the object or disable the renderer of it and move it far our of reach. Why these 2 option depends if you want to reuse the item again faster, because Instantiate and Destroy are expensive calls.

There is no code sorry, but I feel that the general idea is more important and the code is up to the person who wants this to work.

Improvements
Possibly, don’t always check through arrays. Put collision on your items and only search when there is a trigger between the item and your player. Your items can have more functionality. Don’t need to restrict to the number convention I have.
I have recently created my own inventory and works for how I want it. Still could be better. You could make a very generalize “Items” then have another “ItemInit” class which keeps track of multiple, same name objects. To help organize. SO, for most games, Health potions are “stackable” so instead of have 5 slots for the 5 potions, you could have 1 slot with 5 potions available.