Argument out of range

Hi everybody

I’ve written two scripts for the aggro system for my MMORPG. They both work great in-game but Unity keeps telling me that the argument in line 32 of EnemyAggro and line 50 of AttackScript is out of range. How can I fix this problem?

EnemyAggro:

import System.Collections.Generic;

var players : List.<GameObject> = new List.<GameObject>();
var aggros : List.<int> = new List.<int>();
var highestaggro : int = -1;
var target : GameObject = null;
var enemyhealth : EnemyHealth;
var storedenemyhealth : int;

function Start () {
enemyhealth = gameObject.GetComponent(EnemyHealth);
storedenemyhealth = enemyhealth.enemyhealth;
}

function OnTriggerEnter (trigger : Collider){
if(trigger.tag == "Player"){
players.Add(trigger.gameObject);
aggros.Add(0);
}
}



function Update (){
if (storedenemyhealth != enemyhealth.enemyhealth){
storedenemyhealth = enemyhealth.enemyhealth;
AggroCheck ();
}
}


function AggroCheck (){
for (i = 0; i <= aggros.Count; i++){
if (aggros *> highestaggro){*

target = players*;*
highestaggro = aggros*;*
}
}
}
AttackScript:
var cooldown : boolean = false;
var interval : float;
var activetexture : GUIStyle;
var inactivetexture : GUIStyle;
var usedtexture : GUIStyle;
var uooscript : Uoo;
var uoo : GameObject;
var enemyhealth : EnemyHealth;
var buttonneeded : String;
var hotkeyselectionscript : HotkeySelectionScript;
var root : GameObject;
var enemyaggro : EnemyAggro;
var checkednumber : int = -1;

function Start (){
root = gameObject.GetComponent(Transform).gameObject;
hotkeyselectionscript = root.GetComponent(HotkeySelectionScript);
}

function Update(){
uooscript = gameObject.GetComponent(Uoo);
uoo = uooscript.uoo;
if (uoo != null){
enemyhealth = uoo.GetComponent(EnemyHealth);
enemyaggro = uoo.GetComponent(EnemyAggro);
}
if (hotkeyselectionscript.skill1 != null){
if (hotkeyselectionscript.skill1 == “Attack”){
buttonneeded = “1”;
}
}
if (hotkeyselectionscript.skill2 != null){
if (hotkeyselectionscript.skill2 == “Attack”){
buttonneeded = “2”;
}
}
if (buttonneeded == “1” || buttonneeded == “2”){
if (Input.GetKeyDown(buttonneeded) && uooscript != null && uoo != null && cooldown == false){
enemyhealth.enemyhealth -= 50;
cooldown = true;
Checklist ();
Timer();
}
}
}

function Checklist () {
for(i = 0; i < 1;){
checkednumber += 1;
if(enemyaggro.players[checkednumber] == gameObject){
enemyaggro.aggros[checkednumber] += 10;
i = 1;
}
}
}

function Timer (){

if(cooldown == true){
usedtexture = inactivetexture;
yield WaitForSeconds(interval);
cooldown = false;
usedtexture = activetexture;
}

Because in the beginning when there is no aggro added to your list the for loop is executed although you got an empty list.

  for (i = 0; i <= aggros.Count; i++){

(i is 0, aggros.Count is 0 → so the loop gets executed)

if (aggros *> highestaggro){*

(we have no aggros[0] yet → argument out of range error!)
If you got a List with a .Count of say 5 you have elements list[0] to list[4] since lists and arrays start with index 0 not 1.
So your for loop should look like this:
for(i = 0; i < aggros.Count; i++)