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 ...

Get Unity 4.x Game Development by Example Beginner's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.