RaycastHit2D to gameobject C# - Solved -

Hi,
im trying to figure how i can click with mouse and when mouseclick hits Gameobject it does something
I have BoxCollider2d on target Gameobject, but i can’t get this working.

void Update ()
    {
        if (Input.GetMouseButtonDown(0))
        {
            RaycastHit2D hit = Physics2D.Raycast(cam.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
            if (hit == humanButton.collider2D)
            {
                print ("hit");
            }
        }

Close

i found the problem. My script was correct but the crosshaircursor was behind UIButton(humanbutton).
When i changed Canvas render mode to Screen Space - Camera and added sorting layer i got my crosshair(mouse) front of UIButton. After this my script worked. Im not sure if i get some problems when my UI canvas isnt anymore ScreenSpace -OverLay