2D raycast doesn't work with trigger colliders?

I’m using this code to scan for a mouse hovering over a sprite

hitInfo = Physics2D.GetRayIntersection(Camera.main.ScreenPointToRay(Input.mousePosition));

If my sprite has a boxcollider2d and isTrigger is not checked, it works. But if I make the collider a trigger, it never returns anything. The same goes for OnMouseEnter, which is why I was trying to use the raycast method. How can I test if a mouse is hovering over a sprite with a trigger collider?

I think you want something like this instead:


RaycastHit hit;
if (Physics2D.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit)){}

Raycasts hit triggers, unless you turn that off in preferences → Physics2D → and uncheck query triggers.