So this code is crashing Unity only sometimes...

So I’ve narrowed it down to the while loop by commenting it out and testing it. So I have no idea why but some times it works in play mode every time, then I exit play mode, re enter play mode and it freezes up and I have to manually end the application.

		if (BlueSquareClicked == true && Physics.Raycast (camRay, out hit, camRayLength, BlueMoveMask)) 
		{
			Debug.Log ("Clicked on BlueMoveTile");

			startPoint = transform.position;
			endPoint = hit.transform.position;

			BlueSquareMoving = true;

			float journeyLength = Vector3.Distance (startPoint, endPoint);
			float distCovered = (Time.time - startTime) * moveSpeed; 
			float journey = distCovered / journeyLength;

			while (BlueSquareMoving == true) 
			{
				Debug.Log ("Here");
				//BlueSquareMoving = false;
				transform.position = Vector3.Lerp (startPoint, endPoint, journey);
				//Debug.Log ("Moving");

				if (transform.position == endPoint) 
				{
					Debug.Log ("Finished Moving");

					BlueSquareMoving = false;
					BlueSquareDoneMoving = true;
					BlueSquareClicked = false;

					GameManager.killTiles = true;
				}
			}
		}

Any Help would be awesome. If I need to post the entire script let me know.

Shouldn’t while (BlueSquareMoving == true) be an if statement? There are two cases for what’s happening in the loop. Either journey >= 1 and it’s run exactly once since BlueSquareMoving gets set to false. Or journey < 1 and it will keep on setting transform.position to the same value over and over in an infinite loop(causing your freeze). I’d replace that loop with:

         if(BlueSquareMoving == true) 
         {
             transform.position = Vector3.Lerp (startPoint, endPoint, journey);
             if (journey >= 1.0f) 
             {
                 BlueSquareMoving = false;
                 BlueSquareDoneMoving = true;
                 BlueSquareClicked = false;
                 GameManager.killTiles = true;
             }
         }