The anatomy of a loop

An iterative loop always starts with the for keyword. It has three important sections:

  • Where to start
  • Where to end
  • What to do after every loop finishes

In this case, we start by declaring an iterator variable called i of type int (integer), and set that iterator i to zero.

var i:int = 0

Next, we say that we're going to loop as long as the value of i is less than (<) the value of rows. Because we've already set rows to 4, this code will loop four times.

i<rows

In the third section, we increase the value of i by one. Here's how the interpreter chews through our loop:

  1. Set an integer variable called i to 0.
  2. Check to see if i is less than rows (4). 0 is less than 4, so let's go!
  3. Run the code inside the loop.
  4. When we're finished, increase ...

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.