Quantcast
Viewing latest article 5
Browse Latest Browse All 137

Trying to solve an issue with a while loop to search to see if numbers are the same. Will explain in description.

I'm going to try to explain this the least confusing way possible. In previous methods I created the array - pieces[]. I added gameObjects according to their tag to the array. Below I have the script that is causing the problem. I know what is causing the problem. In my while loop I have || usedNumbers.Contains(iter)). When I added this, it caused an infinite loop. Otherwise, the code works great. Well the idea that I was looking for was -1- that a number could not be repeated, -2- that an object couldn't assign itself its own number, or -3- an object could not be assigned a number of a gameObject if it's number was assigned there already. 3 is a mouthful but that is what I am looking to fix. "I am gameObject 3, number 1 has been assigned to me, but gameObject 1 has already been assigned number 3, so I need to repeat the loop." while( usedNumbers.Contains( newNumber ) || newNumber == iter || usedNumbers.Contains(iter)) If I could find an alternate solution to this, that would be awesome. I was wondering about making a list of the iterations that had already been ran and do it about the same as I did with usedNumbers. I will do that now as I wait for a reply. I bet it's something easy that I am just overlooking. Edit: I tried writing the new list the same as my previous, but adding the current iter to it and it was still an infinite loop. usedNumbers.Contains(iter) I still think this should work and not cause an infinite. void AssignChildren () { int pieceCount = pieces.Length; List usedNumbers = new List(); int newNumber; for (int iter = 0; iter < pieceCount; iter++) { newNumber = Random.Range (0, pieceCount); while( usedNumbers.Contains( newNumber ) || newNumber == iter || usedNumbers.Contains(iter)) { newNumber = Random.Range( 0, pieceCount); } PieceScript assignPieceChild = pieces [iter].GetComponent (); assignPieceChild.whoDoIControl = newNumber; usedNumbers.Add( newNumber ); } }

Viewing latest article 5
Browse Latest Browse All 137

Trending Articles