Problem with touch button (when moved)

I got this script for android intput it works fine but the problem comes when the player holds the button and then slides the finger out of it; the button stays pressed even tho the finger is not over it, it stays pressed even when the user doesnt have a finger over the screen(after the sliding thing). How may I solve this issue? Any idea?

public Texture2D pressed, notPressed;
public Rigidbody2D Player;

	void Update(){
		this.guiTexture.pixelInset = new Rect(0, 0, Screen.width/3.5f, Screen.height/6f);

		SpeedController playerScript = Player.GetComponent<SpeedController>();
		if (Input.touches.Length <= 0) {
			playerScript.move = 0;
		} else 
		{
			
			for (int i= 0; i < Input.touchCount; i++) {
				
				if (this.guiTexture.HitTest(Input.GetTouch(i).position))
				{
					if(Input.GetTouch(i).phase == TouchPhase.Began){
						playerScript.left = true;
						this.guiTexture.texture = pressed;
					}
					if(Input.GetTouch(i).phase == TouchPhase.Ended){
						playerScript.left = false;
						this.guiTexture.texture = notPressed;

					}
				}
				
				
				
			}
		}
	}
}

Try this :

    for (int i= 0; i < Input.touchCount; i++) 
    {  
        if (this.guiTexture.HitTest(Input.GetTouch(i).position))
        {
             if(Input.GetTouch(i).phase == TouchPhase.Began)
             {
                playerScript.left = true;
                this.guiTexture.texture = pressed;
             }
             if(Input.GetTouch(i).phase == TouchPhase.Ended)
             {
                playerScript.left = false;
                this.guiTexture.texture = notPressed;
             }
        }
        else 
            playerScript.left = false;
    }

If you hold down the button and release it outside the HitTest area, then playerScript.left remains always TRUE because

if (this.guiTexture.HitTest(Input.GetTouch(i).position))

doesn’t apply anymore. So you need a … = false statement to stop the movement once you’re out of the Touch area.

using UnityEngine;

public class TouchInput : MonoBehaviour
{

Ray ray;
RaycastHit hit;
Rigidbody rb;

public bool left;
public bool right;
public bool jump;
public int speed;

void Start()
{
    rb = GameObject.FindGameObjectWithTag("player").GetComponent<Rigidbody>();
    speed = 1;
}

void Update()
{
    if (left)
    {
        rb.AddForce(-10*speed, 0, 0);
    }
    if (right)
    {
        rb.AddForce(10*speed, 0, 0);
    }
    if (jump)
    {
        rb.AddForce(0, 10*speed, 0);
    }
    
    Touch[] myTouches = Input.touches;

    if (Input.touchCount > 0)
    {
        for (int i = 0; i < Input.touchCount; i++)
        {
            ray = Camera.main.ScreenPointToRay(Input.GetTouch(i).position);

            if (Physics.Raycast(ray, out hit, Mathf.Infinity) == true)
            {
                //touch phase has begun
                if (Input.GetTouch(i).phase == TouchPhase.Began)
                {
                    if (hit.collider.tag == "left")
                    {
                        left = true;
                    }
                    else if (hit.collider.tag == "right")
                    {
                        right = true;
                    }
                    else if (hit.collider.tag == "jump")
                    {
                        jump = true;
                    }
                }
                //touch phase has ended for buttons
                else if (Input.GetTouch(i).phase == TouchPhase.Ended)
                {
                    if (hit.collider.tag == "left")
                    {
                        left = false;
                    }
                    else if (hit.collider.tag == "right")
                    {
                        right = false;
                    }
                    else if (hit.collider.tag == "jump")
                    {
                        jump = false;
                    }
                }
            }
            if (Physics.Raycast(ray, out hit, Mathf.Infinity) == false)
            {
                left = false;
                right = false;
                jump = false;
            }
        }
    }
}

}