- Home /
Check Overlap foreach loop not functioning correctly
Should Check if any slot overlaps and change color to different color
setup is each slot is under different canvas with each being changed to sortorder 0 if selected and -1 if not it only reads slots that higher on hierarchy which instead should read every slot no matter where in hierarchy
Script Bellow
public List<Slot> AllSlots;
foreach (Slot i in AllSlots)
{
Vector3[] corners = new Vector3[4];
this.GetComponent<RectTransform>().GetWorldCorners(corners);
Rect rec = new Rect(corners[0].x, corners[0].y, corners[2].x - corners[0].x, corners[2].y - corners[0].y);
i.GetComponentInParent<RectTransform>().GetWorldCorners(corners);
Rect rec2 = new Rect(corners[0].x, corners[0].y, corners[2].x - corners[0].x, corners[2].y - corners[0].y);
if (rec.Overlaps(rec2))
{
i.GetComponent<RawImage>().color = Color.red;
}
else
{
i.GetComponent<RawImage>().color = Color.gray;
}
}
Comment