Inventory issue, container is null

Hello!
I hope everyone is doing well!

I am having issues with the inventory course. Currently, I’m on step 3 “Inventory Backing Store”. I imported the package to a test project and it works fine (as expected) however, I tried to integrate the code into my own project and it does not work.
The problem is that when I try to move an item to another slot, it resets to the original position when I drop it

I have tracked down the issue, and it is on this method on the DragItem class:

private IDragDestination<T> GetContainer(PointerEventData eventData)
        {
           
            if (eventData.pointerEnter)
            {
                var container = eventData.pointerEnter.GetComponentInParent<IDragDestination<T>>();

                return container;
            }
            return null;
        }

The problem is the method GetComponentInParent is returning null always. I tried doing this:

 if (eventData.pointerEnter)
            {
                var container = eventData.pointerEnter.GetComponentInParent<IDragDestination<T>>();

               //If we can't find the container in the parent, look for it in the gameobject
                if (container == null)
                {
                    container = eventData.pointerEnter.GetComponent<IDragDestination<T>>();
                }

                return container;
            }
            return null;

But it did not work.
As you can see, my InventorySlot prefab has the right script:

And the InventorySlotUI implements the right interface too:

 public class InventorySlotUI : MonoBehaviour, IDragContainer<InventoryItem>

So I have no idea why is this happening, and the strangest thing is it USED to work, when I integrated the code from step 2 and the dragging and dropping of items worked only in the UI, it worked excellently and I was able to move items around
But now, it just doesn’t work, any ideas?

I have a repository on github with my project in case anyone would want to take a look, let me know.

Thanks!

Hmmm, the first thing that comes to mind is the possibility that another UI overlay is overtop the InventoryUI… For example, the Fader can interfere with the drag scripts if RaycastTarget is checked on it’s image.

Ah, that sounded very promising. I have actually a Fader canvas and the HUD from the last course, however, I tried disabling both and the behavior is still the same, the item resets to the position and the container is still null.

I have also tried printing the game object name of eventData.pointerEnter.gameobject and I get “InventorySlot (clone)” as expected

 Debug.Log(eventData.pointerEnter.gameObject);

I did the same on the pristine project (the one I downloaded from Gamedev’s resources) and I get the same game object name, so I figure this is OK.

I have made another discovery though
If I try changing the generic T class for my InventoryItem class, It looks like the component is not null

var container2 = eventData.pointerEnter.GetComponentInParent<IDragDestination<InventoryItem>>();

Debug.Log(container2);

not null component

But I don’t think I should be really doing that, a lot of other methods depend on that type, and if I change it everything breaks.
I even tried casting it like this, but it becomes null again!

return container2 as IDragDestination<T>;

That is all I have, for now, I’ll keep digging but it is getting frustrating, could it be a bug?

Thanks again!

Zip up your project and upload it to https://gdev.tv/projectupload Be sure to remove the Library folder to conserve space. (As easy as it sounds to clone repos, that usually tends to take a lot more time and bandwidth on my end).

Thank you Brian, I just uploaded my project

I don’t think I would have thought to look for this one without the project in hand…
For some reason, your InventoryDragItem was

public class InventoryDragItem : DragItem<Sprite>

instead of

public class InventoryDragItem: DragItem<InventoryItem>

This means that the Drag system was using T = typeof(Sprite) instead of T=typeof(InventoryItem)
So GetComponentInParent<T>() becomes GetComponentInParent<Sprite>()

1 Like

Yes!! That was it

I cannot believe I missed that, I must have reviewed the code a hundred times and still did not see it :sweat_smile:

Thank you very much Brian!!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms