Touch not working properly

Guys After looking so many answers on forum and trying every solution I am not able to figure out the problem in my code, I am making aan Android Game, which 1 joystick and 1 fire button in the screen

I am trying to implement a feature to select enemies with touch, and ignore a particular region of the screen to avoid touch, that is UI buttons… problem is the touch passes through the UI buttons and is not working.
following is my code.

ignoreFireButton=new Rect(Screen.width-but.sizeDelta.x-Screen.width/12,
Screen.height-but.sizeDelta.y-Screen.height/9,
buttonImage.sizeDelta.x+Screen.width/12,
buttonImage.sizeDelta.y+Screen.height/9);
//ignoreFireButton=but.rect;
//t=canvas.pixelRect;

ignoreJoystick=new Rect (0,
Screen.height-JoystickImage.sizeDelta.y-Screen.height/10,
JoystickImage.sizeDelta.x+Screen.width/10,
JoystickImage.sizeDelta.y+Screen.height/10);

///////////////////////////////////////////
//Random Code
//////////////////////////////////////////

void OnTouchScreen()
{
int touchpos=0;

if(Input.touchCount>=1)
{
//touchpos=Input.GetTouch(0).position;

for(int i=0;i<Input.touchCount;i++)
{
if (!ignoreFireButton.Contains(Input.GetTouch(i).position)&&
!ignoreJoystick.Contains(Input.GetTouch(i).position))
{
touchpos=i;
break;
}
}
//Debug.Log(“”+touchpos);
//DebuggTextBar
text.text=“”+touchpos+“”+Input.GetTouch(touchpos).position+“”+ignoreJoystick;

origin=newVector3(Input.GetTouch(touchpos).position.x,Input.GetTouch(touchpos).position.y,0);
//origin=touchpos;
ray=Camera.main.ScreenPointToRay(origin);

if(Physics.Raycast(ray,out hit,15.0f))
{
if(hit.transform.tag==“Enemy”)
{
Selected=hit.transform;
//text.text=“”+Selected;
}
if(hit.transform.tag !=“Enemy”)
{
Selected=null;
//text.text=“”+Selected;
}
}

}
}

Simplified Code

ignoreFireButton = new Rect (
                                            Screen.width-buttton.sizeDelta.x - Screen.width/12,
                                           Screen.height - but.sizeDelta.y - Screen.height/9 ,
                                           button.sizeDelta.x + Screen.width/12,
                                           button.sizeDelta.y + Screen.height/9);
// ignoreFireButton = but.rect;
        // t = canvas.pixelRect;

ignoreJoystick = new Rect (0 ,
                                            Screen.height - JoystickImage.sizeDelta.y- Screen.height/10,
                                            JoystickImage.sizeDelta.x + Screen.width/10,
                                             JoystickImage.sizeDelta.y + Screen.height/10);
void OnTouchScreen ()
    {
        int touchpos = 0;

        if (Input.touchCount >= 1)
        {
    //        touchpos = Input.GetTouch (0).position;


            for (int i = 0; i < Input.touchCount; i++)
            {
                if (!ignoreFireButton.Contains(Input.GetTouch (i).position) && !ignoreJoystick.Contains (Input.GetTouch (i).position))
                {
                    touchpos = i;
                    break;
                }
            }
    //        Debug.Log ("" + touchpos);
            // Debugg Text Bar
            text.text = "" +  touchpos + " " + Input.GetTouch (touchpos).position + " " +ignoreJoystick;

            origin = new Vector3 (Input.GetTouch (touchpos).position.x, Input.GetTouch (touchpos).position.y, 0);
//            origin = touchpos;
            ray = Camera.main.ScreenPointToRay (origin);

                if (Physics.Raycast (ray, out hit, 15.0f)) {
                    if (hit.transform.tag == "Enemy") {
                        Selected = hit.transform;
    //                    text.text = "" + Selected;
                    }
                if (hit.transform.tag != "Enemy")
                {
                        Selected = null;
//                        text.text = "" + Selected;
                }
            }
           
        }
    }

I still need solutions :frowning: