Unpacking the code
If you saw a similar line of code in the previous chapter and were a little mystified by it, here is a clearer, longer-form breakdown of what's happening (do not add this code to your script):
var someRandomIndex:int = Random.Range(0,aEmptySquares.Count);
Choose a random number between 0 and the length of the aEmptySquares
array minus one. For example, if the aEmptySquares
array has 5 squares in it, we'll get a number between 0 and 4.
square = aEmptySquares[someRandomIndex];
Retrieve a square from the aEmptySquares
array at the index point we randomly chose. For example, if the randomly chosen number between 0 and 5 was 2, then retrieve the element at index 2 of the aEmptySquares
array.
The line we actually used in our script is just those two lines above, condensed into a single line:
square = aEmptySquares[Random.Range(0,aEmptySquares.Count)];
Why go through all this trouble to compose a list of empty squares, and then randomly choose one from the list? Why don't we just...