Unable to Equip item from script

GAaron

New member
Hi there,

I was wondering why I'm getting this error when tryin to equip from a script Inventory access:

Error: Unable to equip item with ItemType Katana (Opsive.UltimateCharacterController.Inventory.ItemType) - the ItemType hasn't been added to the inventory.

I've even tried accessing the Inventory's ItemTypes directly:

C#:
gameObject.GetComponent<Inventory>().EquipItem(gameObject.GetComponent<Inventory>().GetAllItemTypes()[1];


but to no avail, is there something I'm doing wrong? I have tried forceEquip on .Pickup as well but nothing happens.
 
It looks like you are trying to equip an item before it has been added to the inventory. You need to add that item first with Inventory.AddItem. The item can be added by doing it within the editor or using runtime pickups.
 
I've tried that:

Code:
gameObject.GetComponent<Inventory>().AddItem(katanaShieldItem, true);

gameObject.GetComponent<Inventory>().PickupItemType(katanaShieldItemType, 1, 1, true, true);

allItemTypes = gameObject.GetComponent<Inventory>().GetAllItemTypes();

gameObject.GetComponent<Inventory>().EquipItem(allItemTypes[1], 1, true);
 
You are getting that error because the ItemType doesn't exist within m_ItemTypeItemMap. The ItemType is added to the map within AddItem so something is likely going wrong there. As an example take a look at ItemPickup.DoItemPickup - this is where the runtime pickups are added and this also allows for adding new items/item types to the inventory.
 
I'm also trying to equip an item from a script and all it does is hide the current weapon and nothing else. Here is the code I am using:
Code:
if(Input.GetKey(KeyCode.L))
        {
            var itemTypes = inventory.GetAllItemTypes();//current inventory
            inventory.UnequipItem(itemTypes[2], 0);
            inventory.EquipItem(itemTypes[0], 0, true);
        }

I tried without the unenquip as well. I know both items are in the inventory since I'm using the demo scene and picked up the items at runtime.

Thanks!
 
Thanks! Never thought to look under the AI tab of the manual.
That's a good point - I'll add it to the EquipUnequip page as well :)


I haven't looked at DoItemPickup yet but I'm trying to equip my item without an animation using the previously mentioned method, is this possible?
Yes - when you take a look at how DoItemPickup works you'll have a better idea. @drewradley is also correct, though you will need to first ensure the item is within the inventory before it can be equipped.
 
Top