I am making a drag and drop inventory system, and so far, it’s going pretty well. But I have ran into a crazy and annoying issue. When an item is picked up, it is displayed in the inventory. The item can be dragged to the 2nd, 3rd, or 4th slot of the 4 slot inventory, but if I try to drag it back to the 1st inventory slot, it does not work. Here is the code:
void IEndDragHandler.OnEndDrag(PointerEventData eventData) {
if(DisplayItem == null)
return;
//Set Slot back to original position
IconImage.rectTransform.position = originalPos;
IconImage.transform.parent.SetSiblingIndex(previousIndex);
Transform parent = IconImage.transform.parent.parent;
Slot[] slots = new Slot[parent.childCount];
for(int i = 0; i < slots.Length; i++) {
Transform slotTransform = parent.GetChild(i);
Slot slot = slotTransform.GetComponent<Slot>();
float width = slot.IconImage.rectTransform.rect.width;
float height = slot.IconImage.rectTransform.rect.height;
Vector2 slotPos = slot.transform.position;
float distance = Vector2.Distance(eventData.position, slotPos);
print("NAME:" + slot.name + " - " + distance);
if(distance < 100.0f) {
transform.SetSiblingIndex(i);
IconImage.rectTransform.position = slot.IconImage.rectTransform.position;
}
}
}
I know the problem is that when the Item isn’t in the 1st slot, on the line where I am printing out the distance, the same exact distance is printed twice, yet with different game object names. So please help me figure out why I can’t drag back to the first slot!
Thanks,
Kyle
EDIT:
I have just found out that if there is 2 items in the inventory, then items can only be dragged to the 3rd and 4th slots, and not the 1st and 2nd slots. And if there are 3 items then only slot 4… So on and so on. I really have no idea what is happening anymore. If more code is needed, please, let me know!