Help pls. I have an endless while loop and I dont know why.

This is my LevelGenerator and please tell me, what I missed in the Code :

#pragma strict

import System.Collections.Generic;

var Feld : GameObject;
var Point : GameObject;
var List : List.<GameObject>;
var c : int;
var Direction : float;
var Test : int = 0;
var Test2 : String = "";

function Start () {


List.Add(Point); // List 1

var ListLength : int = 1; // ListLength 1
for (var i : int = 0; i<20; i++)
{


Instantiate(Feld); // Feld
c = 0; // c 0
do{
Direction = Random.Range(0,4);
Test = 0;
c = 0; // jedes mal ein Reset c
if (Direction < 1 ){
Feld.transform.localPosition = Vector3(Point.transform.localPosition.x,Point.transform.localPosition.y + 10,Point.transform.localPosition.z);
}else if (Direction < 2){
Feld.transform.localPosition = Vector3(Point.transform.localPosition.x,Point.transform.localPosition.y - 10,Point.transform.localPosition.z);
}else if (Direction < 3){
Feld.transform.localPosition = Vector3(Point.transform.localPosition.x - 10,Point.transform.localPosition.y,Point.transform.localPosition.z);
}else if (Direction < 4){
Feld.transform.localPosition = Vector3(Point.transform.localPosition.x + 10,Point.transform.localPosition.y,Point.transform.localPosition.z);
}



Test = ListLength;

for (var i2 : int = 0; i2 < ListLength;  i2++)
{
if (Feld.transform.localPosition.x == List[i2].transform.localPosition.x && Feld.transform.localPosition.y == List[i2].transform.localPosition.y){
c = 1;
}

}

} while(c == 1);

Point.transform.localPosition.x = Feld.transform.localPosition.x;
Point.transform.localPosition.y = Feld.transform.localPosition.y;

List.Add(Feld);

ListLength = ListLength + 1;

}

}

Please use code tags when posting code.

1 Like

Sry, I edited it.

On line 46 you set c as 1 and on line 51 you have a while c == 1 without ever changing c from 1. Not sure if this is causing the problem.

Don’t be afraid to put some debug lines in your code to step through it.

We can’t really see everything that is happening, but I am assuming that you set c = 0; at the start. When it goes into your while loop and it steps through the for statement the first (and only) if statement at some point becomes true. This causes C becomes 1 and it never gets changed again back to anything else, thus an infinite loop will occur.

For whatever reason when you are looping through your list, that if statement is becoming true at least once (always) and sets C to 1;

C is getting the value of 1 and never change, thats what cause the infinit loop.