How to find out which gameobject my mouse points to

What is the best way to figure out which gameobject I am pointing my mouse at? I am aware of OnMouseDown but that only works if the script is attached to the gameobject itself and I dont want that. Is it possible to find out which gameobject exactly I am pointing at with a raycast? I know I can make out the collider but does that also point out to the gameobject it belongs to?

1 Answer

1

Create a raycast using camera.ScreenPointToRay and use the hit.point

instead of hit.point, I guess you could go with hit.collider.gameObject (or something along those lines).