[BUG] Item restriction error

My restricts item of category "Hat" then I drag and drop the hat into it and instead of snapping it back to the collection it came from there is an error and the hat disappears.

NullReferenceException: Object reference not set to an instance of an object
Opsive.UltimateInventorySystem.UI.Grid.ItemShapeGridData.AddItem (Opsive.UltimateInventorySystem.Core.DataStructures.ItemInfo itemInfo, Opsive.UltimateInventorySystem.Core.InventoryCollections.ItemCollection receivingItemCollection) (at Assets/Opsive/UltimateInventorySystem/Scripts/UI/Grid/ItemShapeGridData.cs:749)
Opsive.UltimateInventorySystem.UI.Grid.ItemShapeGridData.AddItemToPosition (Opsive.UltimateInventorySystem.Core.DataStructures.ItemInfo itemInfo, UnityEngine.Vector2Int position) (at Assets/Opsive/UltimateInventorySystem/Scripts/UI/Grid/ItemShapeGridData.cs:772)
Opsive.UltimateInventorySystem.UI.Grid.ItemShapeGridData.AddItemToPosition (Opsive.UltimateInventorySystem.Core.DataStructures.ItemInfo itemInfo, System.Int32 index) (at Assets/Opsive/UltimateInventorySystem/Scripts/UI/Grid/ItemShapeGridData.cs:796)
Opsive.UltimateInventorySystem.UI.Grid.ItemShapeGrid.AddItem (Opsive.UltimateInventorySystem.Core.DataStructures.ItemInfo itemInfo, System.Int32 index) (at Assets/Opsive/UltimateInventorySystem/Scripts/UI/Grid/ItemShapeGrid.cs:254)
Opsive.UltimateInventorySystem.UI.Item.DragAndDrop.DropActions.ItemViewShapeDropAction.Drop (Opsive.UltimateInventorySystem.UI.Item.DragAndDrop.ItemViewDropHandler itemViewDropHandler) (at Assets/Opsive/UltimateInventorySystem/Scripts/UI/Item/DragAndDrop/DropActions/ItemViewShapeDropAction.cs:71)
Opsive.UltimateInventorySystem.UI.Item.DragAndDrop.ItemViewDropActionsWithConditions.Drop (Opsive.UltimateInventorySystem.UI.Item.DragAndDrop.ItemViewDropHandler itemViewDropHandler) (at Assets/Opsive/UltimateInventorySystem/Scripts/UI/Item/DragAndDrop/ItemViewSlotDropActionSet.cs:235)
Opsive.UltimateInventorySystem.UI.Item.DragAndDrop.ItemViewSlotDropActionSet.HandleItemViewSlotDrop (Opsive.UltimateInventorySystem.UI.Item.DragAndDrop.ItemViewDropHandler itemViewDropHandler) (at Assets/Opsive/UltimateInventorySystem/Scripts/UI/Item/DragAndDrop/ItemViewSlotDropActionSet.cs:116)
Opsive.UltimateInventorySystem.UI.Item.DragAndDrop.ItemViewDropHandler.HandleItemViewSlotDropInternal () (at Assets/Opsive/UltimateInventorySystem/Scripts/UI/Item/DragAndDrop/ItemViewDropHandler.cs:259)
Opsive.UltimateInventorySystem.UI.Item.DragAndDrop.ItemViewDropHandler.HandleItemViewSlotDrop (Opsive.UltimateInventorySystem.UI.Item.ItemViewSlotEventData dropSlotEventData) (at Assets/Opsive/UltimateInventorySystem/Scripts/UI/Item/DragAndDrop/ItemViewDropHandler.cs:249)
Opsive.UltimateInventorySystem.UI.Item.ItemViewSlotsContainerBase+<>c__DisplayClass78_0.<Initialize>b__7 (UnityEngine.EventSystems.PointerEventData pointerEventData) (at Assets/Opsive/UltimateInventorySystem/Scripts/UI/Item/ItemViewSlotsContainerBase.cs:275)
Opsive.UltimateInventorySystem.UI.CompoundElements.ActionButton.OnDrop (UnityEngine.EventSystems.PointerEventData eventData) (at Assets/Opsive/UltimateInventorySystem/Scripts/UI/CompoundElements/ActionButton.cs:264)
UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IDropHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:92)
UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:272)
UnityEngine.EventSystems.EventSystem:Update() (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/EventSystem.cs:501)
 
Is it possible you do not have the latest version or that you changed some of the source code?
Your error line does not match mine.

The only way you could get an error in that function is if the ItemShapeGridData was not initialized by an ItemShapeGridController.
 
That's odd, I double checked and the this script was not changed on my end since the last update...
Please confirm your version by looking at your Inventory System Manager:
1677255017701.png


I would also recommend double checking that your ItemShapeGridData is correctly referenced by the ItemShapeGridController.
If it is, and you are still getting an error, I would recommend waiting for the next update which is planned for the end of next week.
 
Yeah the version is correct. I have 1 controller and 3 item shape grid datas on it correctly setup, evertything works well with the inventories except the restriction.

I can wait until next week in thar case...
 
Hi, I updated to the latest versions and here's the error Im getting when I drag an item that shouldnt go into a collection

NullReferenceException: Object reference not set to an instance of an object
Opsive.UltimateInventorySystem.UI.Grid.ItemShapeGridData.AddItem (Opsive.UltimateInventorySystem.Core.DataStructures.ItemInfo itemInfo, Opsive.UltimateInventorySystem.Core.InventoryCollections.ItemCollection receivingItemCollection) (at Assets/Opsive/UltimateInventorySystem/Scripts/UI/Grid/ItemShapeGridData.cs:719)
Opsive.UltimateInventorySystem.UI.Grid.ItemShapeGridData.AddItemToPosition (Opsive.UltimateInventorySystem.Core.DataStructures.ItemInfo itemInfo, UnityEngine.Vector2Int position) (at Assets/Opsive/UltimateInventorySystem/Scripts/UI/Grid/ItemShapeGridData.cs:742)
Opsive.UltimateInventorySystem.UI.Grid.ItemShapeGridData.AddItemToPosition (Opsive.UltimateInventorySystem.Core.DataStructures.ItemInfo itemInfo, System.Int32 index) (at Assets/Opsive/UltimateInventorySystem/Scripts/UI/Grid/ItemShapeGridData.cs:766)
Opsive.UltimateInventorySystem.UI.Grid.ItemShapeGrid.AddItem (Opsive.UltimateInventorySystem.Core.DataStructures.ItemInfo itemInfo, System.Int32 index) (at Assets/Opsive/UltimateInventorySystem/Scripts/UI/Grid/ItemShapeGrid.cs:254)
Opsive.UltimateInventorySystem.UI.Item.DragAndDrop.DropActions.ItemViewShapeDropAction.Drop (Opsive.UltimateInventorySystem.UI.Item.DragAndDrop.ItemViewDropHandler itemViewDropHandler) (at Assets/Opsive/UltimateInventorySystem/Scripts/UI/Item/DragAndDrop/DropActions/ItemViewShapeDropAction.cs:71)
Opsive.UltimateInventorySystem.UI.Item.DragAndDrop.ItemViewDropActionsWithConditions.Drop (Opsive.UltimateInventorySystem.UI.Item.DragAndDrop.ItemViewDropHandler itemViewDropHandler) (at Assets/Opsive/UltimateInventorySystem/Scripts/UI/Item/DragAndDrop/ItemViewSlotDropActionSet.cs:235)
Opsive.UltimateInventorySystem.UI.Item.DragAndDrop.ItemViewSlotDropActionSet.HandleItemViewSlotDrop (Opsive.UltimateInventorySystem.UI.Item.DragAndDrop.ItemViewDropHandler itemViewDropHandler) (at Assets/Opsive/UltimateInventorySystem/Scripts/UI/Item/DragAndDrop/ItemViewSlotDropActionSet.cs:116)
Opsive.UltimateInventorySystem.UI.Item.DragAndDrop.ItemViewDropHandler.HandleItemViewSlotDropInternal () (at Assets/Opsive/UltimateInventorySystem/Scripts/UI/Item/DragAndDrop/ItemViewDropHandler.cs:259)
Opsive.UltimateInventorySystem.UI.Item.DragAndDrop.ItemViewDropHandler.HandleItemViewSlotDrop (Opsive.UltimateInventorySystem.UI.Item.ItemViewSlotEventData dropSlotEventData) (at Assets/Opsive/UltimateInventorySystem/Scripts/UI/Item/DragAndDrop/ItemViewDropHandler.cs:249)
Opsive.UltimateInventorySystem.UI.Item.ItemViewSlotsContainerBase+<>c__DisplayClass78_0.<Initialize>b__7 (UnityEngine.EventSystems.PointerEventData pointerEventData) (at Assets/Opsive/UltimateInventorySystem/Scripts/UI/Item/ItemViewSlotsContainerBase.cs:275)
Opsive.UltimateInventorySystem.UI.CompoundElements.ActionButton.OnDrop (UnityEngine.EventSystems.PointerEventData eventData) (at Assets/Opsive/UltimateInventorySystem/Scripts/UI/CompoundElements/ActionButton.cs:264)
UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IDropHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:92)
UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:272)
UnityEngine.EventSystems.EventSystem:Update() (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/EventSystem.cs:501)
 
Ah I see now. The GetItemCollectionToAddItemTo is returning a null item collection.

I added a null check just above the error line hopefully that will fix your issue:

Code:
public virtual ItemInfo AddItem(ItemInfo itemInfo, ItemCollection receivingItemCollection)
{
    if (receivingItemCollection == null) {
        if (m_OnlyTryAddToFirstCollection) {
            return m_ItemCollectionGroup.AddItem(itemInfo);
        } else {
            var itemCollection = GetItemCollectionToAddItemTo(itemInfo);
            if (itemCollection == null) {
                return ItemInfo.None;
            }
            return itemCollection.AddItem(itemInfo);
        }
    }
    if (m_ItemCollectionGroup.Contains(receivingItemCollection)) {
        return receivingItemCollection.AddItem(itemInfo);
    }
    return ItemInfo.None;
}


I'm unsure if I should make CanAddItem return false if the the GetItemCollectionToAddItemTo function returns null by default. or if I should check if the Inventory still wants to add it.
For now lets try just with the code change I mentioned above and let me know if you still have problems
 
The error I get now is this. The item still disappears once dropped into the restricted collection.

NullReferenceException: Object reference not set to an instance of an object
Opsive.UltimateInventorySystem.UI.Grid.ItemShapeGridData.TryPlaceItemToPosition (Opsive.UltimateInventorySystem.Core.DataStructures.ItemInfo info, UnityEngine.Vector2Int position) (at Assets/Opsive/UltimateInventorySystem/Scripts/UI/Grid/ItemShapeGridData.cs:302)
Opsive.UltimateInventorySystem.UI.Grid.ItemShapeGridData.AddItemToPosition (Opsive.UltimateInventorySystem.Core.DataStructures.ItemInfo itemInfo, UnityEngine.Vector2Int position) (at Assets/Opsive/UltimateInventorySystem/Scripts/UI/Grid/ItemShapeGridData.cs:747)
Opsive.UltimateInventorySystem.UI.Grid.ItemShapeGridData.AddItemToPosition (Opsive.UltimateInventorySystem.Core.DataStructures.ItemInfo itemInfo, System.Int32 index) (at Assets/Opsive/UltimateInventorySystem/Scripts/UI/Grid/ItemShapeGridData.cs:766)
Opsive.UltimateInventorySystem.UI.Grid.ItemShapeGrid.AddItem (Opsive.UltimateInventorySystem.Core.DataStructures.ItemInfo itemInfo, System.Int32 index) (at Assets/Opsive/UltimateInventorySystem/Scripts/UI/Grid/ItemShapeGrid.cs:254)
Opsive.UltimateInventorySystem.UI.Item.DragAndDrop.DropActions.ItemViewShapeDropAction.Drop (Opsive.UltimateInventorySystem.UI.Item.DragAndDrop.ItemViewDropHandler itemViewDropHandler) (at Assets/Opsive/UltimateInventorySystem/Scripts/UI/Item/DragAndDrop/DropActions/ItemViewShapeDropAction.cs:71)
Opsive.UltimateInventorySystem.UI.Item.DragAndDrop.ItemViewDropActionsWithConditions.Drop (Opsive.UltimateInventorySystem.UI.Item.DragAndDrop.ItemViewDropHandler itemViewDropHandler) (at Assets/Opsive/UltimateInventorySystem/Scripts/UI/Item/DragAndDrop/ItemViewSlotDropActionSet.cs:235)
Opsive.UltimateInventorySystem.UI.Item.DragAndDrop.ItemViewSlotDropActionSet.HandleItemViewSlotDrop (Opsive.UltimateInventorySystem.UI.Item.DragAndDrop.ItemViewDropHandler itemViewDropHandler) (at Assets/Opsive/UltimateInventorySystem/Scripts/UI/Item/DragAndDrop/ItemViewSlotDropActionSet.cs:116)
Opsive.UltimateInventorySystem.UI.Item.DragAndDrop.ItemViewDropHandler.HandleItemViewSlotDropInternal () (at Assets/Opsive/UltimateInventorySystem/Scripts/UI/Item/DragAndDrop/ItemViewDropHandler.cs:259)
Opsive.UltimateInventorySystem.UI.Item.DragAndDrop.ItemViewDropHandler.HandleItemViewSlotDrop (Opsive.UltimateInventorySystem.UI.Item.ItemViewSlotEventData dropSlotEventData) (at Assets/Opsive/UltimateInventorySystem/Scripts/UI/Item/DragAndDrop/ItemViewDropHandler.cs:249)
Opsive.UltimateInventorySystem.UI.Item.ItemViewSlotsContainerBase+<>c__DisplayClass78_0.<Initialize>b__7 (UnityEngine.EventSystems.PointerEventData pointerEventData) (at Assets/Opsive/UltimateInventorySystem/Scripts/UI/Item/ItemViewSlotsContainerBase.cs:275)
Opsive.UltimateInventorySystem.UI.CompoundElements.ActionButton.OnDrop (UnityEngine.EventSystems.PointerEventData eventData) (at Assets/Opsive/UltimateInventorySystem/Scripts/UI/CompoundElements/ActionButton.cs:264)
UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IDropHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:92)
UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:272)
UnityEngine.EventSystems.EventSystem:Update() (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/EventSystem.cs:501)
 
Yes, that's what I feared, Let's add a null check here too.

Code:
/// <summary>
/// Try to add an item in the position.
/// </summary>
/// <param name="info">The item info.</param>
/// <param name="position">The position.</param>
/// <returns>True if the item was added.</returns>
public virtual bool TryPlaceItemToPosition(ItemInfo info, Vector2Int position)
{
    //Debug.Log($"Try Add: {position} + {info}");
    var x = position.x;
    var y = position.y;
    if (info.Item == null) {
        return false;
    }
    
    if (info.Item.TryGetAttributeValue<ItemShape>(ShapeAttributeName, out var shape) == false


It all comes down from what I mentioned in the previous post about CanAddItem.
So lets add the Null Check here too.

Code:
/// <summary>
/// Can the item be added at index?
/// </summary>
/// <param name="itemInfo">The item info to add.</param>
/// <param name="index">The index to add to item to.</param>
/// <returns>True if the item can be added.</returns>
public override bool CanAddItem(ItemInfo itemInfo, int index)
{
    var canAddBase = base.CanAddItem(itemInfo, index);
    if (canAddBase == false) { return false; }
    var gridDataCollection = m_ItemShapeGridData.GetItemCollectionToAddItemTo(itemInfo);
    if (gridDataCollection == null) {
        return false;
    }
    if (Inventory.CanAddItem(itemInfo, gridDataCollection) == null) { return false; }
    var itemPos = m_ItemShapeGridData.OneDTo2D(index);
    if (m_ItemShapeGridData.IsPositionAvailable(itemInfo, itemPos) == false) {
        return false;
    }
    return true;
}

I hope that fixes your issues
 
I couldnt find the last method you linked at least the base class.

No errors now but my item disappears into the void. I think its supposed to go back to where it came from if it cannot be added.
 
CanAddItem is the on in ItemShapeGrid.cs around line 227.

Out of curiousity, what ItemViewDropAction are you using? Are you using "ItemViewShapeDropAction"?
If not you should make sure to use it.

Just to be clear, this is what I am refering to:
1679593232288.png
 
Yes looks exactly the same.

Edit: I updated the ItemShapeGrid and now it seems to be working. Also now I see red outlines when I drag the item to the restricted collection unlike prior to that last update. I will keep testing tomorrow and let you know if a bug appears.
 
Last edited:
Top