How to ignore a tap on a Button in Android? [SOLVED]

Hello everyone,
First off let me apologize if this has been answered somewhere, but all of the answers I am finding seem to be a bit older and possibly deprecated. I’d like to find the most straight forward answer to this question.

I am making a game in which the character jumps when the mouse button (0) is clicked or, on Android, when the screen is tapped.

Currently I am wrapping my input code in

if(!EventSystem.current.IsPointerOverGameObject())
{}

On mac/pc, this works as expected, if you click the screen your character jumps but if you click on a UI element the click only passes to the button and the character input is ignored. On Android, the character continues to receive input in the scene through UI elements (although the UI elements also work).

Basically, this is causing unwanted input when pausing/unpausing the game through the UI button, as well as unwanted input when UI windows are open.

Thanks in advance to anyone who has found an easy way to work around this!

I want to be able to retain the on mouse controls for desktop, I simply want taps to do what they normally do unless the tap is on a UI element.

Seems really simple; frustrating that I’m having such a hard time finding a clear answer on this. I’m not saying Unity doesn’t have the documentation on this, I’m just saying I can’t seem to find it. Anyone?

Anyone? Surely this is a common thing that people are dealing with?

I couldn’t help too much without seeing exactly how input is handled on both devices, is it the same code for desktop and mobile?

Off the top of my head, instead of checking that event system stuff, maybe have an “invisible” button which takes up the entire screen area as the highest button in the canvas hierarchy, thus making it the furthest “back” of all the buttons, then the visible buttons below it in the hierarchy, so this way a click or tap works as expected, I think the “closer” buttons intercept the input and block taps to the large button behind them. Maybe try that out…

Edit: oh right also, make those buttons call a method in some manager script, that way your not listening for events but just directly calling some method/function from the buttons built in uhh… stuff…

Good luck!

Thanks for the feedback, but surely this is not an optimal method.

How can unity not have a simple and direct way for a button to not receive input as the rest of the screen does? This seems like such a basic feature that should be included (and maybe is) in Unity, but I can’t for the life of me find anything useful regarding this.

Anyone have any useful info on this? Millions of touchscreen apps in the world and this is a basic feature that I cannot seem to find a simple way of fixing in Unity…

Literally the controls ate GetMouseButtonDown(0), GetMouseButton(0), and GetMouseButtonUp(0). I just need these to perform the way they are intended (which works as a tap and a tap and hold on touchscreen) UNLESS the tap or click is on a button, in which case i just need the associated button function to be called.

Again, working on desktop. How do I do this on Touch?

You might want to use a level of indirection above what you have.
You can try using CrossPlatformInput, e.g.

I guess make an if statement that checks if any button has been clicked before assuming the screen elsewhere has been clicked… again I’m not totally sure how the logic is working on your code, but the way you describe it, I’d imagine that canvas trick would work fine on mobile… can’t really come up with much more useful advice, sorry.

1 Like

Again, appreciate your input, but that seems like a duct tape method to me. I thought one of the selling points of Unity was to “code once” and deploy to all these platforms? I’m so frustrated. /opens beer.

This was just a little bug fix on my to do list today, but instead I’ve spent my whole day working on this. Have tried a dozen outdated solutions that no longer work. I’ve found countless q/a posts and threads with people trying to fix this EXACT problem but not one simple solution that works.

Dear Unity, how do I click a UI button on touchscreen without registering a GetMouseButtonDown(0)? People have been asking this question for two years. What is the answer?

Well it is one of the aims of unity and it’s design, but some cases require special treatment. I wish unity could just read my mind and write lean code to do exactly what I think about haha.

Anyway give my duct tape method a shot. You’ll find the UI stuff is pretty performant, and even doing strange stuff like that it works pretty well. You’ll notice putting like panels or any images in front of buttons or other elements will block input to the stuff behind. This is how I figure it would solve your problem, because buttons and other ui stuff works for a mouse or touch input. Even if it doesn’t work perfectly I’d say it’s worth trying out before disregarding the idea :stuck_out_tongue:

1 Like

I’m sure your method would work, but adding a blank button over the whole screen is not efficient. I am looking for a real fix, not something that will just “get the job done”. I am trying to learn how to do this right and want to use best practices, and this seems like something that the majority of games on mobile will need to address.

How are studios currently handling this? I literally need something that will do exactly what if(!EventSystem.current.IsPointerOverGameObject()) does, only for touch.

Ok so I found a solution that is working here. In the Heirarchy, under EventSystem, I unticked the Touch Input Module (deactivated it) and in the Standalone Input Module, I ticked the “Allow Activation On Mobile Device” checkbox.

Being that this game does not use multitouch, that is all that was needed. Two check boxes.

Still doesn’t really answer how I’d address this if the game was using more complex multi-touch controls, but it works 100% for this application.

2 Likes

Awesome, wish everything was so easy to fix!

I marked this as solved, but am still looking for the best answer to this… the solution I found will work for an application that only uses taps, but if I were using multi-touch controls this would not necessarily solve the issue I am having. I still think Unity needs to revisit the UI system to make this easier for mobile.

it is a fucking joke that Unity fucked this up.

Consider how much money e’re paying them. it’s laughable.

thanks for your tip above.

Did you in fact stumble on BoredMormon’s solution – I mean his “third” solution

Look for his video here …

I’ll be interested to hear if that works fine for you in Android - I think it will. Cheers

1 Like

For anyone who stumbles upon this, the right solution is:

EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId)
3 Likes

yes the exact same issue i have faced and no there is no proper solution one solution i figured out possibly be neglecting the process here gun rotation when the touch is in certain area something like this :-

mouse_pos = Input.mousePosition;
if (alec.GetComponent<new_player_controller>().facing_right == true)
{
if (mouse_pos.x < stageDimensions.x + (stageDimensions.x) * 26 && mouse_pos.y < stageDimensions.y + 180) return;

          Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) -transform.position;
                float rotZ_ = Mathf.Atan2(-difference.y, -difference.x) * Mathf.Rad2Deg;
               transform.rotation = Quaternion.Euler(0f, 0f, rotZ_ + offset);
//taking rotation by touch

}