[Bug] Item.Drop: Fix dropping items that have more than one UseableItem

echtnice

Member
Hi,

1. Character controller variant (Ultimate Character Controller, First Person Controller, etc).
Third Person Controller

2. Unity version (include which SRP, beta Unity versions aren't supported)
2019.3.9f1

3. Bug description
- I have the Pistol from the Demo
- I added a ThrowableItem to the Pistol
- If the Character dies, it tries to drop the pistol but in ItemPickup.DoItemIdentifierPickupInternal:55, the last element in m_ItemDefinitionAmounts is null, with a length of three.

I think the error is in Item.Drop. The last for loop sets the
itemDefinitionAmounts to index (consumableItemIdentifiers + 1) which is always 1 because it is not updated.


If I change Item.Drop:1160-1172 to the following, it works for me.

Code:
                            for (int i = 0; i < m_ItemActions.Length; ++i) {
                                if ((usableItem = (m_ItemActions[i] as UsableItem)) != null && (consumableItemIdentifier = usableItem.GetConsumableItemIdentifier()) != null) {
                                    var consumableDropCount = 0;
                                    // Only remove the remaining inventory if there is just one ItemIdentifier remaining. This will allow the character to keep the consumable ammo
                                    // if only one item is dropped and the character has multiple of the same item.
                                    if (count == 1) {
                                        consumableDropCount = m_Inventory.GetItemIdentifierAmount(consumableItemIdentifier);
                                    }
                                    var remainingConsumableAmount = usableItem.GetConsumableItemIdentifierAmount(); // The count may be negative (for use by the UI).
                                    var consumableDefinitionAmount = GenericObjectPool.Get<ItemDefinitionAmount>();
                                    consumableDefinitionAmount.Initialize(consumableItemIdentifier.GetItemDefinition(), consumableDropCount + (remainingConsumableAmount > 0 ? remainingConsumableAmount : 0));
+                                    consumableItemIdentifiers++;
+                                    itemDefinitionAmounts[consumableItemIdentifiers] = consumableDefinitionAmount;
-                                     itemDefinitionAmounts[consumableItemIdentifiers + 1] = consumableDefinitionAmount;
                                }

4. Steps to reproduce

5. The full error message (if any)
NullReferenceException: Object reference not set to an instance of an object
Opsive.UltimateCharacterController.Objects.CharacterAssist.ItemPickup.DoItemIdentifierPickupInternal (UnityEngine.GameObject character, Opsive.UltimateCharacterController.Inventory.InventoryBase inventory, System.Int32 slotID, System.Boolean immediatePickup, System.Boolean forceEquip) (at Assets/Opsive/UltimateCharacterController/Scripts/Objects/CharacterAssist/ItemPickup.cs:55)
Opsive.UltimateCharacterController.Objects.CharacterAssist.ItemPickupBase.DoItemIdentifierPickup (UnityEngine.GameObject character, Opsive.UltimateCharacterController.Inventory.InventoryBase inventory, System.Int32 slotID, System.Boolean immediatePickup, System.Boolean forceEquip) (at Assets/Opsive/UltimateCharacterController/Scripts/Objects/CharacterAssist/ItemPickupBase.cs:254)
Opsive.UltimateCharacterController.Objects.CharacterAssist.ItemPickupBase.DoItemPickup (UnityEngine.GameObject character, Opsive.UltimateCharacterController.Inventory.InventoryBase inventory, System.Int32 slotID, System.Boolean immediatePickup, System.Boolean pickupItemIdentifier) (at Assets/Opsive/UltimateCharacterController/Scripts/Objects/CharacterAssist/ItemPickupBase.cs:216)
Opsive.UltimateCharacterController.Objects.CharacterAssist.ItemPickupBase.TryItemPickup (Opsive.UltimateCharacterController.Inventory.InventoryBase inventory, System.Int32 slotID) (at Assets/Opsive/UltimateCharacterController/Scripts/Objects/CharacterAssist/ItemPickupBase.cs:115)
Opsive.UltimateCharacterController.Objects.CharacterAssist.ItemPickupBase.TriggerEnter (UnityEngine.GameObject other, System.Int32 slotID) (at Assets/Opsive/UltimateCharacterController/Scripts/Objects/CharacterAssist/ItemPickupBase.cs:104)
Opsive.UltimateCharacterController.Objects.CharacterAssist.ItemPickupBase.TriggerEnter (UnityEngine.GameObject other) (at Assets/Opsive/UltimateCharacterController/Scripts/Objects/CharacterAssist/ItemPickupBase.cs:82)
Opsive.UltimateCharacterController.Objects.CharacterAssist.ObjectPickup.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Opsive/UltimateCharacterController/Scripts/Objects/CharacterAssist/ObjectPickup.cs:167)
 
Top