Clean your code, so it is easier to follow.
for( var hit in colliders )
{
if( hit.rigidbody )
{
hit.rigidbody.AddExplosionForce( explosionPower, transform.position,
explosionRadius );
}
}
What is the "for()" doing?
It loops through every collider in colliders. The variable hit is for every loop assigned to the next collider. It is the same as:
for( var i = 0; i < colliders.Length; ++i )
{
var hit = colliders[i]; // This is your "var hit in colliders"
if( hit.rigidbody )
{
hit.rigidbody.AddExplosionForce( explosionPower, transform.position,
explosionRadius );
}
}
Is it like a "if()"?
No, but it does have a scope (the { and }) so it looks very much like an if. The for will repeat the code inside the scope for every item in your collection.
What is asked in the if statement "if(hit.rigidbody)"
It checks if the collider has a rigid body. It is the same as calling "if(hit.rigidbody != null)".
Is "hit" a list of things that have collided with something?
No, hit is one of the objects that you have in your collider collection. Since it is in a loop, hit changes each time the loop is run so it will repeat for every object in your collider collection. Just think that "hit" is going to be "all" your colliders, one at a time in the loop.
Is that why the code says "hit.rigidbody.AddExplosionForce()" to add a explosion force to rigidbodys that have been hit?
Yes, it applies an explosion force to every rigid body that was attached to a collider in the collision test.
I think you could do some simple tests to boost your confidence in what the code is doing. Try this:
function Start()
{
// Create a collection of strings
var collection = new String[3];
// Put some data in it
collection[0] = "Hello";
collection[1] = "For";
collection[2] = "Loop";
print ("Test 1");
// Test the for with enumeration
for (var each in collection)
{
print ("each = " + each);
}
print ("Test 2");
// Test the for with explicit control,
// backwards for fun!
for (var i = collection.Length - 1; i >= 0; --i)
{
var each = collection[i];
print ("i = " + i);
print ("each = " + each);
}
}
The output for the first test:
Test 1 each = Hello each = For each = Loop
and the second test:
Test 2 i = 2 each = Loop i = 1 each = For i = 0 each = Hello