Why is this Do/While loop hanging Unity?
Hey folks. Hoping some fresh eyes will spot the issue here. Following code hangs Unity:
private void AssignPairs()
{
int currentNumberOfPairs = currentBoardSettings._posTargetsArraySize / 2; // All the board sizes are even numbers, so can be halved to find current number of pairs
currentPairTags = new PairTypeTag[currentNumberOfPairs]; // Create a new array to contain this round's legitimate tags
for (int i = 0; i < currentPairTags.Length; i++)
{ // Initialise the array of legitimate tags this round to EMPTY00 - blank
currentPairTags[i] = PairTypeTag.EMPTY00;
}
for(int i = 0; i < currentPairTags.Length; i++)
{ // Iterate through the legit tag slots...
if(currentPairTags[i] == PairTypeTag.EMPTY00)
{//... until you find a slot marked empty.
bool uniqueTag = false; // Create a flag for the while loop, initialised to false;
do // While the flag is false, loop...
{
Debug.Log("Choosing candidate...");
PairTypeTag candidatePairTag = (PairTypeTag)Random.Range(1, 15); // Choose a random tag from all the enums, excluding EMPTY00
Debug.Log("Looking through current pair slots...");
// uniqueTag = true;
for (int foo = 0; foo < currentPairTags.Length; i++)
{ // Look through the array of legit tags...
if (candidatePairTag != currentPairTags[foo]) // If the candidate and the current legitTagSlot are NOT the same...
{
currentPairTags[foo] = candidatePairTag; // ...assign the tag to the current legitTagSlot...
uniqueTag = true; // ...and mark the uniqueTag as true, so we don't have to loop again, hopefully not locking up.
}
else
{
Debug.Log("Failed to select pair list");
uniqueTag = true;
}
}
uniqueTag = true;
} while (uniqueTag == false);
Debug.Log("Holy shit, we got out of that While loop!");
}
}
I'm trying to randomly select a few enums and enter them into an array, then use this array as a source list to assign pairs of enums (0-15) to cards - basically I'm creating a Concentration game.
However, I cannot seem to stop my While and/or Do/While loops from hanging Unity! The quoted code sets uniqueTag to true no matter the outcome of the for loops (a test) and STILL the damned thing hangs.
Anybody see what I'm not seeing?
--Rev
In testing, I commented out the most nested For loop -
//for (int foo = 0; foo < currentPairTags.Length; i++)
//{ // Look through the array of legit tags...
// Debug.Log("Beep three times");
//if (candidatePairTag != currentPairTags[foo]) // If the candidate and the current legitTagSlot are NOT the same...
//{
// //currentpairtags[foo] = candidatepairtag; // ...assign the tag to the current legittagslot...
// uniqueTag = true; // ...and mark the uniquetag as true, so we don't have to loop again, hopefully not locking up.
// Debug.Log("Iterating through code.");
//}
// else
// {
// Debug.Log("Failed to select pair list");
// uniqueTag = true;
//}
//}
... and the code ran without hanging. But if I reenable the For loop - even with all of its contents commented out, so it's just an empty loop - Unity hangs.
I am confused as all hell.
--Rev
Answer by Oribow · Apr 11, 2016 at 09:48 AM
In your last for loop you are incrementing the wrong value. Instead of I++, write foo++, or you getting an infinite loop.
Your answer
Follow this Question
Related Questions
While loop causing Unity to hang up? 1 Answer
While Loop not working when clicked on Play button 1 Answer
Why does this while loop freeze unity? 1 Answer
While loop freeze 0 Answers