How to unequip an item through code?

Finnt

New member
Hello, I tried to unequip an item through code like this:

equipper.UnEquip(itemInfo.Item);

But I get a NullReferenceException in the UnEquip Method of the Equipper script at the line 196. The line says:

if (ObjectPoolBase.IsPooledObject(itemObject.gameObject)) {

Unequipping does work through the inventory UI, but my game requires, that the Item is used while it is equipped. I tried to do this by:
1. Unequipping the item
2. Removing the item from the inventory

Maybe there is another way to use an item, like through an ItemAction, but I did not find it in the documentation and also I would like this usage to be only accessible through code, not in the inventory UI for the player. Also the action only would need to do these two things and I thought, since these are methods of the equipper and iventory, I might be able to avoid writing my own ItemAction and triggering it only through code, while (somehow? maybe?) specifying, that it should not be an option in the inventory UI.

I understand, that this post might not contain all the relevant information, but I am not sure which information would be necessary. Please let me know which information I should communicate.

Thank you!
 
The Equipper is mainly used to visually equip items. It does so by monitoring what items are added/removed from the Equipement ItemSlotCollection.

So normally you shouldn't have to worry about unequipping items. Simply remove the item from the ItemSlotCollection and it wil automatically unequip it.

You'll find some code examples here:
and here
and here:


My guess is that you are getting an exception error because you are trying to unequip an item that is not visually equipped. By removing the item instead of unequipping it directly it should do the checks to avoid this error.

You might be interested in the docs here too:
 
Hello, thank you for your response. It helped me understand the function of the equipper component. Now I just remove the item and there is no exception.

However there is a problem resulting from this. If I try to remove an Item from a stack, the stack does not decrease and the item is not visually unquipped. I think that it does not get unequipped is because there are still items in the stack. I don't know why the item stack does not get decreased. I do specify the amount that should be removed in code.

I thought I could avoid dealing with this problem by changing the Item Category to be unique. That way I will not have to deal with stacking. However this also does not work, because the Item does not get removed, just like if it was in a stack.

The only way I have found, that removes the Item is by removing the whole Item Info. When there are stacks, this removes all items in the stack, which I don't want. But when I set the category to unique, I can remove the unique item this way.

Do you know how I could achieve my desired behaviour of stacked items, where one item gets removed from the stack?

For now, I will just change the Item Categories, so that the Items don't stack anymore and remove the ItemInfo from the inventory, because I don't have more time to figure out how to solve this problem at the moment.

Thanks for your help!
 
Last edited:
Sorry for the delay, I was out on vacation.

I'm sorry I don't understand what you are trying to do.

You have ItemStacks lets say 3 Apples. If you remove 2 Apples it should still have 1 Apple.
At that point nohing should change right?

From there if you remove another 1 Apple, then you are left with nothing, so the ItemStack should be removed.
Just to be clear you are not editing the ItemStack directly right? You are using the remove function of the ItemSlotCollection right?
 
Back
Top