touch appear let go dissapear?

hello, i have this code and it works so when i touch the object it will move it around but what im trying to do is when i touch anywhere that object will appear where i touch and follow the drag and when i let go the object dissapears, any help is greatly appreciated, thank you

class dragTransform : MonoBehaviour

{

    private Color mouseOverColor = Color.blue;

    private Color originalColor = Color.yellow;

    private bool dragging = false;

    private float distance;

    

    void Start()

    {

        

    }

    void OnMouseEnter()

    {

        renderer.material.color = mouseOverColor;

    }

 

    void OnMouseExit()

    {

        renderer.material.color = originalColor;

    }

 

    void OnMouseDown()

    {

        dragging = true;

        distance = Vector3.Distance(transform.position, Camera.main.transform.position);

    }

 

    void OnMouseUp()

    {

        dragging = false;

    }

 

    void Update()

    {

        if (dragging)

        {

            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            Vector3 rayPoint = ray.GetPoint(distance);
			
            transform.position = rayPoint;
			transform.eulerAngles = new Vector3(90,Camera.main.transform.eulerAngles.y,0);
			

        }

    }

}

Try this:

class DragTransform : MonoBehaviour {
    public float distance = 10.0f;  // Distance in front of the camera to place the object
	
	void Start() {
		renderer.enabled = false;	
	}

    void Update() {
		Vector3 v3Pos;
        if (Input.GetMouseButtonDown (0)) {
			v3Pos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance);
			transform.position = Camera.main.ScreenToWorldPoint(v3Pos);	
			renderer.enabled = true;
		} else if (Input.GetMouseButton (0)) {
			v3Pos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance);
			transform.position = Camera.main.ScreenToWorldPoint(v3Pos);		
		}
		else if (Input.GetMouseButtonUp (0)) {
			renderer.enabled = false;	
		}
    }
}

You can use Input.GetMouseButtonDown(0) and Input.GetMouseButtonUp(0) to detect whether or not a mouse button is pressed or depressed anywhere on the screen.

Then you can either make methods “OnMouseDown” and “OnMouseUp” public and call them directly when the button is pressed or you can make separate methods(which is better because if you click your object, they will fire twice).

Or you can use SendMessage to call them, but it is slower.