- Home /
DoWhile Loop is crashing Unity. Why?
The do while loop within the following script crashes Unity every time I try to run the scene. If I take the loop out of this script it runs fine, but with it in, Unity just freezes when I hit play. Any idea why?
using UnityEngine;
using System.Collections;
public class FloorDistance : MonoBehaviour {
public GameObject characterPos;
public GameObject floorPos;
Vector3 posChar;
Vector3 posFloor;
public float distance;
void Awake ()
{
posChar = characterPos.transform.position;
posFloor = floorPos.transform.position;
}
void Update()
{
do
{
distance = Vector3.Distance(posChar, posFloor);
} while(distance > 0f);
Debug.Log(distance);
}
}
Answer by BlueRaja_2014 · Feb 10, 2014 at 09:20 PM
Vector3.Distance(posChar, posFloor)
does not change within your loop. Thus if it's > 0f
, the loop will go on forever.
Ah, thanks. How do I make it change? posChar is assigned to a character that can move around the scene and posFloor is assigned to a static object. I want to update the float "distance" with the distance between the two objects as I move the character around.
Answer by Linus · Feb 10, 2014 at 09:19 PM
distance will never be more than 0, it will never change from what it is currently when the loop starts.
Your answer
Follow this Question
Related Questions
Unity Crashes when opened 1 Answer
A node in a childnode? 1 Answer
When I create a script on mac, unity crashes! 1 Answer
Why does unity keep crashing when I use this script 3 Answers
[Closed]Unity crash, while/for loop 3 Answers