Detect tap location of UI Button in OnClick()

I’m trying to determine the location of the tap within a UI Button so I can spawn a game object at that point. It’s a large tap area and I’d like to spawn a small animation at the point of the tap for a visual effect.

Is it possible to get the tap location inside the OnClick handler?

I had assumed something like this would work:
if (Input.touchCount > 0) {
Touch touch = Input.GetTouch(0);
Vector2 touchPos = Camera.main.ScreenToWorldPoint(touch.position);
}
…but Input.touchCount is 0.

Hi - isn’t this a UI question = more like UI forum question.

“I’m trying to determine the location of the tap within a UI Button”
“Is it possible to get the tap location inside the OnClick handler?”

Not sure what you are asking, as are using touch and not button / pointer click handler?

You can use Input.mousePosition, or UI system to get screen position:

Depending on your setup (which you didn’t tell anything about) you might have to convert your screen space position.

Sorry for the lack of detail.

Here’s the setup.
I have a UI->Canvas and a UI->Button inside of it.
I created a script with an OnClick() handler and attached it to the button.

Whenever I click the button, my script gets called, so that works.

But inside that OnClick handler, Input.touchCount is always 0.

My assumption was that since the user just clicked the button, that touchCount would be greater than 0 (because the user is literally touching the button right now)

Nevermind! I figured this out. I had assumed that Input.touchCount would be true for mouse clicks on the PC. As soon as I tapped the screen instead, it worked.

Thanks for your reply.