Detecting Mouse Click on 2D Sprite

Hello, all I’m trying to do is detect if the sprite rectangle contains the mouse position however I have no idea how to get access to a Sprite gameObject’s rectangle. I have the same effect working with GUIText but would like to make this work to make a menu using sprites as buttons.

I’d appreciate any help, thank you.

1 Like

The OnMouseDown/Over/Up functions work as usual. Like all objects except GUIElements, sprites need colliders for that to work.

The only way for this to work, apart from making sure there is Collider component, is to make sure the Z index of the object is on top, even if you are making 2D games. The sorting layer is ignored and Z index take precedence instead.

So this means you may not be able to attach your sprite under another game object. I tried parenting my sprite under another game object and my mouse click is no longer detected.

Add a collider to the game object (preferably 2D) and then call the functions OnMouseDown/Over/Up as @Eric5h5 told you. I want to emphasize that these functions won’t work unless you add a collider component.