Checking which one is closest in a List

So, I have an Inventory, and I want to be able to drag what the inventory has to 5 boxes that you can acces with hotkeys.
But, I use a for loop, and I was wondering if it was possible to check which one of the boxes the mouse X and Y was closest to.
It that possible?
Thanks!

Yes.

One possibility that comes to mind, you could:

int index=Mathf.roundToInt((mouseX-positionOfLeftBox)/widthOfABox);

Um… could you explain that positionofLeftBox and widthofBox a bit…

oh, heres a pic of my inventory.

I dunno if that last one worked

You would replace positionofLeftBox with the x coordinate of the left-most box, and you’d replace widthofBox with the distance from one box to the next.

Ok um, now how would I check whats closest to 0?
Say I had a list

//New list code My_List
My_List[roundToInt(0)];

Would it round to the one closest to zero? c:

bump? :c

help please

My_List[roundToInt(0)];

That would get you the first item in My_List

You said you had a list, is the one on the left the first one in the list, and and the one next to it the second in the list, and so on? How far apart are they?

If they were 10 apart, and started at 40, and you clicked on 87, then 87 - 40 is 47, and 47/10 is 4.7. And 4.7 rounded off is 5. So if your 6th (not 5th because it starts at 0) is positioned at 90, then you’re good.

40
50
60
70
80
90

Yup, 90 is the sixth in that list.

ty c: