unity crashes when i run a empty foreach loop
ignore the 50 nested lists, all the code does is make 25 sets of 3 pairs of coordinates I don't want any of the 3 pairs to be the same. The List<>.Contains doesn't work for nested lists for some reason. The foreach that I want to use to check if the pair already exists instantly crashes unity when run.
List<List<List<int>>> randomizedroomsections = new List<List<List<int>>>();
for (int a = 0; a < 5; a++)
{
for (int b = 0; b < 5; b++)
{
List<List<int>> randomposes = new List<List<int>>();
while (randomposes.Count < 3)
{
List<int> randompos = new List<int>();
randompos.Add(Random.Range(0, 3));
randompos.Add(Random.Range(0, 3));
//the for each below doesn't work
foreach (List<int> randompos1 in randomposes) {
//if (!(randompos1 == randompos))
//{
//randomposes.Add(randompos);
//}
}
}
randomizedroomsections.Add(randomposes);
}
}
String f = "Randomized:";
foreach (List<List<int>> thing1 in randomizedroomsections) {
f = f + "\n";
foreach (List<int> thing2 in thing1) {
f = f + "\n";
foreach (int thing3 in thing2) {
f = f + thing3 + ", ";
}
}
}
Your while loop runs until the element count inside "randomposes" is greater or equal to 3. However inside your while loop you currently do not add anything to that list. Therefore the element count is stuck at 0 and you have an infinite loop.
So it's not the foreach loop that is causing problems since it won't run at all as "randomposes" is empty. Did you actually debug your issue? Just attach visual studio as debugger, set a breakpoint and step through your code. The behaviour should be pretty obvious.
Your answer
Follow this Question
Related Questions
Nested Foreach Loop is freezing/crashing Unity. 0 Answers
My App crashes when I use the correct Game's Ads ID 0 Answers
2019.1.8f1 > Android Crash with GPGS !! 1 Answer
Voxel Chunk error 1 Answer
Unity craches inconsistently on Android during rendering in GfxDeviceGLES::DrawBufferRanges function 1 Answer