- Home /
Problem with while loop looping infinitely
Okay my while loop is going on forever and im not sure why. This is the list I am accessing in this script
public List<int> usedTiles = new List<int>();
And here is the while loop within the other script. Everything is linking properly as far as I know but it simply loops forever. There must be something very obvious that I am not seeing but I'm sure you guys will.
int tileIndex = UnityEngine.Random.Range (0, 8);
while (levelGenerator.usedTiles.Contains (tileIndex))
{
tileIndex = UnityEngine.Random.Range (0, 8);
}
public List usedTiles = new List();
<--That isn't giving you some kind of error to start with? What is usedTiles a List<of> ?
Edit : Never$$anonymous$$d. Thanks for editing your post. 101/010 code button is a pain in the ass sometimes.
there we go, sorry. First time posting and didn't sere how to include code at first
Here's a link to a random-exclude topic :
http://answers.unity3d.com/questions/452983/how-to-exclude-int-values-from-randomrange.html
The way you have it now, if your [usedTiles] has {0,1,2,3,4,5,6,7} in it then it's going to keep going forever because it'll always generate one of those numbers.
Well thats the thing, I understand that it would loop forever with those numbers. But I have it empty. I expect that it would loop forever after exhausting its options, but it seems to loop before doing anything, it doesnt appear to do anything before crashing. Is it because it has nothing declared to it? cause, as i understand it, i've left it empty.
You didn't assign anything to it in the inspector either??
Answer by soulblade · Apr 04, 2014 at 12:03 PM
Try listing your code in the update() function and change 'while' to 'if' that should automatically update the code and test if the used tiles contains the tile index. Hope this helps