Crash on loop using Lerp.

My code crashes whenever I click…

void Update () 
	{
		if (Input.GetMouseButtonDown (0) == true) 
		{
			StartCoroutine(OnLClick());
		}
	}
	IEnumerator OnLClick () 
	{
		yield return new WaitForSeconds(.01f);
		Vector3 stwpCamera = Camera.main.ScreenToWorldPoint (Input.mousePosition);
		while (!(transform.position == stwpCamera))
		{
			transform.position = Vector3.Lerp (transform.position, new Vector3(stwpCamera.x, stwpCamera.y, -10), Time.deltaTime);
		}
	}

Any help would be greatly appreciated.

Do not compare your positions using == in your while because these values are float and it’s unlikely that floats will be exactly equal. Since your two values are not equal your while loop is infinite.

Use Mathf.Approximately to compare floats.

So your while loop condition will be

while(!(Mathf.Approximately(transform.position,stwpCamera)))