UnityScript Variable Scope?

Hi all, I’ve striped down this code to see if anyone can shed some light on what I am not seeing here. The bottom print statement does not output as expected (or at least what I expect). It should output the results from the if statements but instead it outputs another gameobject in the scene, even if invSlot == 1 the output does not match. It works fine inside the if statement just not outside. (note: also does the same thing if I first declare the “var getStats : itemStats” outside of the for loop)

function EquipWeapon(invSlot : int){
    
	var getObj : GameObject;

	for (getObj in WeaponPool){
		var getStats : itemStats = getObj.gameObject.GetComponent(itemStats);
		if (invSlot == 1){
			if (getStats.itemType == inventory.weapon1Stats.itemType){
				print ("getStats.itemName= " + getStats.itemName);
			}
		}
		if (invSlot == 2) {
			if (getStats.itemType == inventory.weapon2Stats.itemType){
				print ("getStats.itemName= " + getStats.itemName);
			}
		}
	}

	print ("getStats.itemName= " + getStats.itemName);

}

This is a simple fix

function EquipWeapon(invSlot : int){
 
    var getStats : itemStats

    for (var getObj in WeaponPool){
       getStats = getObj.gameObject.GetComponent(itemStats);
       if (invSlot == 1){
         if (getStats.itemType == inventory.weapon1Stats.itemType){
          print ("getStats.itemName= " + getStats.itemName);
         }
       }
       if (invSlot == 2) {
         if (getStats.itemType == inventory.weapon2Stats.itemType){
          print ("getStats.itemName= " + getStats.itemName);
         }
       }
    }
 
    print ("getStats.itemName= " + getStats.itemName);
 
}

Also the last print statement will print the last object ever selected in the for loop. Which is the last object in the array/list. You could break out of the for loop after setting the item correctly

    function EquipWeapon(invSlot : int){
     
        var getStats : itemStats
    
        for (var getObj in WeaponPool){
           getStats = getObj.gameObject.GetComponent(itemStats);
           if (invSlot == 1){
             if (getStats.itemType == inventory.weapon1Stats.itemType){
              print ("getStats.itemName= " + getStats.itemName);
              break;
             }
           }
           if (invSlot == 2) {
             if (getStats.itemType == inventory.weapon2Stats.itemType){
              print ("getStats.itemName= " + getStats.itemName);
              break;
             }
           }
        }
     
        print ("getStats.itemName= " + getStats.itemName);
     
    }

The Curly Braces define a Scope { if you declare something here } it gets lost here

  1. put your var getStats : itemStats outside the for loop.
  2. var getStats : itemStats = getObj.gameObject.GetComponent(itemStats); << Error itemStats not defined
  3. for (getObj in WeaponPool) >> for(var Type : varName in somelist/array) >> Error getObj is also undefined in this case

I wonder why your script execute at all ^^

Edited: had wrong Syntax in the for loop