How to use raycast similar to oncollisionenter?

Hi, I’m trying to create a mechanic that allows the player to interact with a button. The way I want to lay it out is by using a raycast and having it call a function on the object that the ray hits.

I was wondering if there is a function similar to oncollisionenter, so that when a ray hits that object, it will call the function attached to the object being hit by the ray.

Not that I know of, but Raycast() comes in a flavor that returns a RaycastHit object.

From that RaycastHit object you can get the GameObject of what you hit, then either look up a Monobehavior on there using .GetComponent<>();, or else just call SendMessage() and invoke your target function that way.