Ignore UI when touching screen

I am trying to move a cursor and have my player shoot when I touch the screen. I have all the code working perfectly with one small exception, when I touch any UI element I don’t want the code to run but it does. I want to only run the code if I touch outside of UI elements.

I’ve been looking around an apparently I’m not the only one with this issue. I’ve tried some of the solutions people have posted but none of them work, maybe they are older solutions? What is really weird is that I can get this to work perfectly when using a mouse, but with touch controls it doesn’t work at all.

Here is the origional code I have been working with:

void Update()
{
   if (Input.touchCount > 0)
   {
      Touch touch = Input.GetTouch(0);

      if (touch.phase == TouchPhase.Began && !EventSystem.current.IsPointerOverGameObject(-1))
      {
         // Do some shooting and stuff
      }
   }
}

So I have tried a ton off different iterations, including but not limited to:

EventSystem.current.IsPointerOverGameObject(Input.touches[0].fingerId))
EventSysten.current.IsPointerOverGameObject()

And even one that first checks a bool:

 private bool IsPointerOverUIObject()
{
   PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current);
   eventDataCurrentPosition.position = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
   List<RaycastResult> results = new List<RaycastResult>();
 EventSystem.current.RaycastAll(eventDataCurrentPosition, results);
   return results.Count > 0;
}

None of these work, no idea why. Any help would be greatly appreciated!

same problem here, any solutions 2021?

:frowning: