Detecting mouse click for the uppermost layered 2d sprite in Unity

I have two sprites. One for the entire background and another small sprite, which is a foreground object. I have written code for both of these objects in their OnMouseDown() functions. When I click on the foreground object, theMouseDown()method of background object also gets called since it is behind the foreground.I just want my foreground sprite to react when I click it, not both foreground and background sprites. Help please?

Generally for this you use different layers for the two sprites, and then two separate raycasts. If you hit the thing you want to block the background, then don’t look for the background layer. Said another way, only look for the background layer when you fail to hit the foreground sprite.