Unity crashing on creation of while loop

This is my code and i need some help. I try to make my gameobject move to the mouse cursor but only when i click and not hold the button.

private void Update()
{
	if (Input.GetMouseButton(0))
	{
		moving = true;
		RaycastHit hit;
		Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
		if (Physics.Raycast(ray, out hit, 100.0f))
		{
			while (moving == true && transform.position != hit.point)
			{
				transform.position = Vector3.MoveTowards(new Vector3(transform.position.x,3f,transform.position.z), hit.point, movementSpeed * Time.deltaTime);
				if (hit.point == transform.position) 
				{
					moving = false;
				}
			}
		}
	}
}

}

Change while to if. Set moving to false before the raycast, and true inside the changed if.

Also, it never breaks out of the loop since you set the start Y value to 3f while leaving the target Y value as whatever the raycast returns.

private IEnumerator moveCoroutine = null ;

private void Update()
 {
     if (Input.GetMouseButtonDown(0))
     {
         RaycastHit hit;
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         if (Physics.Raycast(ray, out hit, 100.0f))
         {
             if( moveCoroutine != null )
                 StopCoroutine( moveCoroutine ) ;
             moveCoroutine = MoveToTarget( hit.point ) ;
             StartCoroutine( moveCoroutine ) ;
         }
     }
 }
 
 private IEnumerator MoveToTarget( Vector3 target )
 {
     while (( transform.position - target ).sqrMagnitude < 0.01f )
     {
         transform.position = Vector3.MoveTowards(new Vector3(transform.position.x,3f,transform.position.z), target, movementSpeed * Time.deltaTime);
         yield return null ;
     }
     moveCoroutine = null ;
 }