Input.GetMouseButtonDown() and UI buttons conflict on mobile phones

Hi,

I have an issue with UI button.

Basically when I tap the screen I want my player to jump (no issues with that).

At the same time, there is a button on the screen which activates a power-up (no issues with that either)
However, by pressing a power-up button makes player jump and activate the power-up function at the same time! (something I don’t want to)

I’ve seen an EventTrigger solution for the button, but it doesn’t work on mobile devices due to touch screen functionality.

Obviously I decided to look for a function which checks if button has been pressed or not and so I got this:

using UnityEngine.EventSystems;
void Update () {
       if (Input.GetMouseButtonDown(0) )
       {
           if (EventSystemManager.currentSystem.IsPointerOverEventSystemObject())
               Debug.Log("left-click over a GUI element!");
           else Debug.Log("just a left-click!");
       }
   }
}

As taken from here: 4.6 - How to detect if any GUI element has been clicked on - Unity Engine - Unity Discussions

But it didn’t help me due to some errors and I’ve also heard that this function is not working in newer Unity versions. (Or method has changed over time).

It would’ve been nice if Unity could detect that automatically without causing such issues. :frowning:

Does anyone know a good solution for preventing Input.GetMouseButtonDown() and UI buttons conflict.
Thanks in advance :slight_smile:

As the EventSystem Input modules use the Input classes as a base there isn’t a way to override this.

The best solution would be to do a ray test with your in-game mouse clicks. If it interacts with the canvas then ignore it, or more granular and test if it hit’s any UI components.

Either that or make your own version of the TouchInputModule to with with your game instead of against it.