Xatoku
1
I’m having a little trouble understanding how foreach works, but I think I’ve almost got it. For a script to return the lowest aggro number, I think this will work but I need some help with getting it to return which gameobject has the lowest number. Something roughly like this:
for aggro in gameobjects
var AggroScript = aggro.GetComponent(“Aggro”)
var aggroAmount = AggroScript.aggroNumber
And then return the gameobject with the lowest aggro. Sorry if it’s a little tough to understand, but I’m new to this. Any help is appreciated.
Tag your Aggro objects as “Aggro” and during Start create an array with them using FindGameObjectsWithTag. Use for…in to sweep the array and find the object you want:
var aggros: GameObject[];
var firstAggro: GameObject;
var firstNum: int;
function Start(){
aggros = GameObject.FindGameObjectsWithTag("Aggro");
firstNum = 10000; // initiate with a big number
for (var aggro: GameObject in aggros){
var aggroScript = aggro.GetComponent(Aggro);
var num = aggroScript.aggroNumber;
if (num < firstNum){
firstNum = num;
firstAggro = aggro;
}
}
}