Physics raycast on 2d button not working

Im following a touch and mouse input tutorial, using raycasts, My project is set to 2D and I have Polygon collider 2d on the button Im making. My touch code is a static singleton class called EventsManager, I have onMouseDown and onMouseUp methods on the button script itself, which works fine. But my eventsManager touch and click code is failing on the line

if (Physics.Raycast(ray, out hit, touchInputMask))

The touchInputMask is a Layer called touch Input and is set correctly for both my button and the public touchInputMask property of my EventsManager class. Ive tried it with is trigger set to false and true on my buttons polygon collider same result, so its not something as simple as that

Altho Im now confused by ContactFilter2D and LayerMask and why LayerMask works Lol its a never ending battle wrapping my head around Unity, I thought this was supposed to make the process of Game creation simpler, I had an easier time when I was making a framework from scratch in Objective C lol ;D

Hi again, I now read your post properly, may I ask what are you trying to do?

"I have Polygon collider 2d on the button Im making"

Are you trying to avoid using UI system altogether and build some custom system using GameObjects and collider2D's?

If you only need buttons, that can be clicked or touched, it will be way easier using UI system. Just add buttons to canvas and that's it. You would only need RayCasting to detect hits in sprites or 3d objects that are not part of UI system.

Yeah I spent a week struggling to get the UI canvas to match the camera size and then get the sprites to be displayed at the proper scale. The standard transform and the sprite render does all that out of the box first time intuitively as you would expect. Then I wasted a further week trying to create a UI heart meter and the amount of code I had to use to get it to scale and position correctly was ludicrous, setting anchorMin anchorMax messing around with the rect transform, just a snippet from what should have been a relatively simple class

And then I want to animate it all with gotween, and from what Ive read, what the Unity canvas and rect transform do behind the scenes is as complicated as the code looks and runs really slow, why is anyones guess, so I figured the best option is to do my own UI and simple have a UIElement parent class with an anchor point -1 to 1 and a xy buffer from the anchor point, which is used in a simple function to convert the transform x and y to different screen ratios. I might be missing something here but it was really unintuitive to me and being made to jump through unnecessary hoops, to get it to pay nice was really testing my sanity.
The only problem Ive got now is using Bitmap fonts with a standard textmesh. If I cant resolve that I'd rather just ditch Unity and go back to my old framework, than struggling with the Unity GUI, it caused me that much grief !

Oops...

"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.