How to add an item by pressing a key?

Hi folks, let's say I wanted to write a script that does the following: add a Sword to the FPC inventory and equip it directly when pressing the button "L". The script is attached to the player.

How can I get the Sword item without having to assign it in the editor? I would like to get an item without assigning it in a serialized field in the editor. I just want to grab it from the ItemDatabase.
inventory.PickupItemType(m_ItemType, m_Count, -1, true, false);
But how to specify ItemType in the script?

I tried something like ItemCollection.ItemTypes[number] but nothing works. What's the correct way to do that?

Help would be much appreciated.


Staff member
You'll want to use that pickup script from the inventory documentation in your new script. Your new script would have to translate the string to an item type which you could do by searching the name of the item type.
Hey Justin,
Thanks for the answer. That was my first thought. But I can't get it done. Can you give me the one line that assigns an item type?

I tried something like
ItemCollection.ItemTypes[x] etc...
But that does not get me what I need.

Maybe I just can't see the wood for the trees.
I found the mistake!
I used the wrong collection. Now I use the one, that is specified in the players SetManager:

ufps_itemCollection = player.GetComponent<ItemSetManager>().ItemCollection;
ufps_ItemType = ufps_itemCollection.ItemTypes[0];
ufps_inventory = player.GetComponent<Inventory>();
ufps_inventory.PickupItemType(ufps_ItemType, m_Count, -1, true, false);

Now it's adding the Assault Rifle to the current inventory.

Thanks for the advice!
Last edited: