Why is rect.overlaps not working

void DropCheck(RectTransform itemImage, RectTransform itemSlot)
{
if (itemImage.rect.Overlaps(itemSlot.rect))
{
print(“touching tips”);

    }
    print("not touching tips");
}

Attempting to check if the player has dragged the image (child of) of an item slot (Both are different UI elements. Parent is a button and child is just for item icons. see below.)

197592-uten-navn.png

I’m so confused as to what I am doing wrong and why they aren’t detecting whetever or not the ItemIcon is inside ItemSlot. I am incredibly frustrated since I just want to have items drop if the player drags the icon outside of the slot and let go…

RectTransform.rect is in local space, so the rects won’t be in the same coordinate system if they have different parents. You need to get the rects in world space so they share the same coordinate system. A simple-ish way of getting the rect in world space is to use the TransformPoint method. Here’s an extension method that does that:

public static class RectTransformExtensions
{ 
    // Use this method like this: someRectTransform.GetWorldRect();
    public static Rect GetWorldRect(this RectTransform rectTransform)
    {
        var localRect = rectTransform.rect;

        return new Rect
        {
            min = rectTransform.TransformPoint(localRect.min),
            max = rectTransform.TransformPoint(localRect.max)
        };
    }
}

Use that method instead of rect in your sample code and you should be fine.