How do I use 'for' to create loops in my script (and what is ! for?)

I'm getting a decent grasp on scripting using javascript but just had a couple of questions i hope someone can clear up a little. Sometimes some variables will have an ! in front of it, just wondering how I can use that. Also I've seen people use the word for in the past ( for(some code goes in here). I've seen both of these used and kind of understand them but would like some clarification if possible. Thanks

The "for" keyword is used to specify a type of "loop" in your code. It enables you to execute some code a specified number of times, or until a certain condition is met. The structure is like this:

for(initializer; test; counter) { ...loop body... }

  • initializer is a statement that is executed at the start of the loop.

  • counter is a statement which is executed at the end of each loop, commonly used to increment a numeric variable.

  • test is a condition which is checked at the start of each loop. if the condition is false, the loop stops executing. This means it is possible for a loop to not execute at all, if the test evaluates to false straight away.

  • loop body is executed each time the loop runs.

For example:

for(var n = 0; n < 10; n++)
{
    Debug.Log("n is now: " + n);
}

This will show (in the console) the numbers zero to nine.

You can also use a 'for' loop to iterate over a collection of items (such as the contents of an array). This takes the form:

for (var itemVar in collectionVar) { ...loop body... }

And this differs slightly in C#, where it's written using the keyword 'foreach':

foreach (itemVar : ItemType in collectionVar) { ...loop body... }

For example, (back to javascript now) to iterate over an array of gameObjects and rotate each one by 90 degrees:

for (var thisGO in gameObjectArray) {
    thisGO.transform.Rotate( Vector3.up, 90 )
}

You can also use this kind of loop to iterate over the immediate children of the current gameobject in the hierarchy, like this:

for (var child in transform)
{
    // do whatever with 'child' transform here
}

(Read more about that here)


The exclamation mark (!) means "not" and is often used to either check for the opposite of a true/false (boolean) variable, or to toggle it to its opposite state. For example:

if (!invicible) {
    ApplyDamage();
}

if (lightSwitchPressed) {
    lightOn = !lightOn;  // toggle true/false
}

Also see: How do I switch a variable each time I hit a key?

! means "not". It returns the opposite of whatever boolean (i.e. true/false) value it's in front of. For example, !true returns false, and !false returns true. It's often used for testing whether a given value is false.

"for" is used to do looping, where you want to execute the same piece of code multiple times. It's often used when you have a collection of objects and want to do something with each item in the collection.

Here's an example using both of them.

var flag : boolean = true;

// Loop ten times. Start i at zero, increment i each time we loop, and stop when i is no longer less than ten.
for (var i = 0; i < 10; i++){
    // Reverse the flag value. If flag is true, this will make it false, and vice versa.
    flag = !flag;

    // If flag is false, print out the current i value.
    if (!flag)
        Debug.Log("false when i = " + i);
}