What does this for loop mean?

Hi Guys,

(1) I just wish to confirm for (taggedEnemy in taggedEnemys) is
actually same as:

for (var taggedEnemy : GameObject in taggedEnemys) 

Because I don’t remember there’s any declaring of taggedEnemy in the earlier code. Please let me know if I get it wrongly.

(2) And we should always use the for (var obj : GameObject in taggedGameObjects) instead of jumping straight to assign the value to the var, right?

Below is the code:

        private var ttag = "enemyAim";
        private var target : Transform;
        
        var closestEnemy : Transform = null;
        var dist : float;
        
        function getClosestEnemy () : Transform
        { 
         var taggedEnemys = GameObject.FindGameObjectsWithTag(ttag);
         var closestDistSqr = Mathf.Infinity;
         var closestEnemy : Transform = null;
         print("get closestEnemy");
         
         for (taggedEnemy in taggedEnemys) // this part (1)
         {
          var objectPos = taggedEnemy.transform.position;
     dist = (objectPos - transform.position).sqrMagnitude;
 
 if (dist < 3.0)
    {
 if (dist < closestDistSqr)
   {
    closestDistSqr = dist;
    closestEnemy = taggedEnemy.transform;
   }
     }
          }
         target = closestEnemy;
        }

Thanks in advance.

EDIT: I added my full code in the for loop code.

  1. yes

  2. yes

Get in the habit of using #pragma strict at the top and setting types on everything. Faster code, more compatible if you want to go mobile