How do I make mouse clicks to be processed only where I need it?

There is an object on the stage (let’s call it the button). So I need that when I poke it with a mouse in the console wrote a message (for example: it works). For this, I wrote the script using GetMouseButtonDown. And added this script to the button. But for some reason when I start the game and I do not click on the button, the console still writes: it works. How do I make mouse clicks to be processed only where I need it?

Just create simple public void and attach it to OnClick in inspector. GetMouseButtonDown check if you clicked anywhere.