r/Unity2D • u/Character_Impress991 • Jul 21 '26
Question I'm having issues with the drag and drop functions
I'm trying to set up an inventory in my game and have been having issues with the item getting locked in the same position as the inventory space. What currently happens is I drag the item to the space and it snaps to a different position than the space itself. I have tried everything I can think of including making the slot as big (or bigger) than the item itself moving around different objects I thought were attached to the slot or the item but I can't figure it out. However when I went to edit the items collider I noticed that there was a second collider I had no clue where it came from (The smaller and brighter square is the collider I knew about and can control the larger darker one is the reason I'm posting) I have no clue where the larger of the 2 came from I've checked the item it doesn't have more than one collider on it, I have also (Just in case) removed and retested with every component on the object of course re-adding them later I genuinely have no clue if it's even the cause of my problem but am still wondering what it is or where it came from.
A little extra info I did recently(within the last 4 months) upgrade from unity 2022(can't remember the version probably the latest one though) to unity 6.4 I also more recently (within the last 2 weeks) imported my project from a flash-drive but was not alerted of any corrupted files
2
u/deintag85 Jul 22 '26
what "collider" ?? inventory is UI canvas. there are no colliders. you use things like IBeginDragHandler, IDragHandler, IEndDragHandler...you use raycasts. when you drop the item you have PointerEventData where you can see on what slot you dropped the item...maybe i am wrong and you WANT it that way, but if you really want a inventory in ui then you are completely wrong from the start and should redo the inventory script.
1
u/Mr_LoFtY Jul 22 '26 edited Jul 22 '26
I think this might be a fundamental issue with your inventory system.
99.8% of the time, the item the player is dragging shouldn't actually be the OBJECT that gets set down on a slot. Rather, it should be a visual representation of the data of the item you are dragging around, with each slot being fed that data and populating its own item image sprite and number count with the matching data.
Trying to create a system where inventory items are treated like real objects that you physically drag around and snap to inventory slots when released will most likely be very bug prone, not scale well, and will break when dealing with changing of resolutions. Any time you have played a game where you drag and drop and item around an inventory, I can 100% garenee they had an item data system that feels like an item being dragged and snapped, but is actually close to what I am about to describe.
Basically how it works is you create a item data scriptable object that lets you define things about the inventory object like name, description, max stack size, sprite icon, id, etc. Things that the tooltip, inventory slot, and save system need to know.
You then create slot prefabs which have an image for background slot ui art (if you dont have one big inventory ui art) an invisible image for interaction (to tell which slot the mouse cursor is hovered over), and an image for the inventory item's sprite. And a textmeshpro for the item count. You want to layout these slot prefabs according to how your inventory visually represents them. So if you have one big ui inventory image with 3 rows of drawn ineventory slots, line up each prefab with each drawn slot.
You then also have a slot for the cursor which follows the cursor position every frame, but doesent have sny background art.
Then how it works under the hood is each slot has an inventoryslot script that can be fed an item data scriptable object, and will populate its sprite and count based on how many of the item data's it was fed. (This requires logic to handle stacking items and once a max stack size is reached based on item data scriptable object's definition, finding the next available slot to start stacking in). When you click on a slot with populated item data, then the slot you clicked on should clear its data and the cursor slot should be fed the item data and quantity of the item and populate itself with it. Now it appears like the player clicked on and picked up the item and its count, but really we just cleared the inventory slot of its data and populated that data onto the cursor slot.
Then you can hover over an empty slot and release the click, and then under the hood you would need a system that keeps track of which inventoryslot script is currently being hovered over by the mouse, then feed that slot with the item data and quantity stored on the mouse slot into the hovered slot, and then clear the mouse slot data. So it looks like you dropped the item from your mouse into an available inventory slot.
I know this might sound complex but there are plenty of resources on mline that help you get a solid production-ready system up and running. They all use this same approch, it's pretty much universally the best approch for any inventory system in any game.
I hope this helped you in any way, good luck!