How to make a sprite clickable?
Using OnMouseDown
The easiest method is to add this function into any script component attached to the gameObject containing the sprite:
void OnMouseDown(){
Debug.Log("Sprite Clicked");
}
The gameObject also need to have a collider. Both 2D and 3D colliders work.
Comparison to other methods
Raycasting only works on one collider type at the time Physics.Raycast
works only against 3D colliders and Physics2D.Raycast
works only against 2D colliders. OnMouseDown
works on both, but it is possible that its performance is as bad as the performance of executing both of the raycasts.
Position based methods stated in other answers are good for performance. Couple of if
statements is much faster to compute than ray casting, but ray casting is still fast enough for basic scenes. The disadvantage of the position checks is that if you change anything (scale or position) you are likely to break your if clauses. Also the if
s are quite complicated if you want sprites to be on top of each other.