Getting the closest Vector3 to the mouse position

I’ve made a function that places vectors in a list making a grid, and I want to somehow highlight which vector or grid that is selected (When the mouse is hovering over it). I tried putting the Vectors in a list and then for each vector I would do the “if distance is less than closest, closest = distance” and than worked when I only hade a few grid but when I made the map bigger it became a problem.

I then started thinking about making like a OverlapSpherethat would get only the closest vectors and then find out which one of those vectors are the closest. But the thing is I don’t know how to make the OverlapSphere only check those vectors. (I can’t really make them into gameobjects due to performance on larger maps i think, not sure about that though).

If you have any other ideas please feel free to tell me.
Thanks.

There is a InBuilt Unity Function that you might want to have a look at.

void OnMouseEnter () {
     // Mouse is now over so change the colour/highlight object
}

void OnMouseExit () {
   // Return to default 
}

You could try making a 2D array instead of list where the indexes represent the Vectors actual position in the grid. That way the mouse position would put you “inside of a box on the grid” and you would just test the four corners for the closest one.

Does that make sense?

Yeah, thanks. I will probably try that tomorrow, sounds great