Unity Crash

while(place==true){
			created[createindex].transform.position= Vector3.Lerp(currentobj.transform.position, spawn.transform.position, Time.time);
			created[createindex].transform.rotation= camera.transform.rotation;
		}

I just added this to my script and now whenever it runs unity crashes. Whats wrong?

You have an infinite loop, since “place” is always true and it’s never set false.

–Eric

Well later in the script, I have it set so that when the user presses a button, place is false, which should end the loop. Is there some reason this doesnt work? All this is located inside a method by the way.

The check for the button press has to be inside the while loop.

It doesn’t matter, since it’s impossible to ever get to “later in the script”, since Unity can never get past the while loop, which you’ve made run infinitely with no way to set the exit condition.

var a = 1;

function Start () {
    // This is an infinite loop.  It runs forever, since "a" is never set to anything else inside the loop
    while (a == 1) {
    }
    print ("You pressed A!");
}

function Update () {
    // This never gets a chance to run, since Unity is busy running the loop in Start forever
    if (Input.GetKeyDown(KeyCode.A)) a = 2;
}

If you make some way for other code to run, then you can create an exit condition, such as using a coroutine:

var a = 1;

function Start () {
    while (a == 1) {
        yield; // Wait a frame
    }
    print ("You pressed A!");
}

function Update () {
    // This can run now, since the while loop yields control every frame
    if (Input.GetKeyDown(KeyCode.A)) a = 2;
}

–Eric

Ok i think i get it now. Just one more thing… In the while loop, I have an if statement that is true if the user presses a button. Could i just set place to false or do i have to use the keyword break