Function is being called even when not told to

I’ll get to the chase, basically I have a box around my character, and whenever that box touches an enemy it adds to a list if it hasn’t been added already. This is so that I can attack multiple enemies at once, and it works great.

But here’s my problem, as soon as I kill an enemy only SOMETIMES does Unity throw and argument exception about the list being out of range from the index I’m using.

And I know exactly what’s causing it, my enemy is dying before I even call the attacking function; which is strange because I haven’t even called the function yet to make it take damage and potentially leading to death.

Here’s my code:

Firstly, I go through a loop for all enemies that are in the list:

if (canAttack && Enemies.Count > 0) {

					for (int i = 0; i < Enemies.Count; i++) {
						if (Enemies  *!= null) {*

_ print ("enemy i is not null name is " + Enemies*.name);_
_ StartCoroutine (WaitForAttack (i, Enemies.transform));
} else {
print (“enemy i is null”);
}
}*_

* }*
Next, I call the “WaitForAttack” coroutine that way the damage is “lined” (lack of better word) up with my attacking animation.

* IEnumerator WaitForAttack(int i, Transform IT)*
* {*
_ print ("i name " + Enemies .name);
* yield return new WaitForSeconds (attackTime);
if (Enemies.Contains (IT) == false) {
print ("null " + IT.name);*_

* } if(Enemies.Contains(IT) == true) {*
* //Enemy is dying before we call the attack function*
_ Attack (Random.Range (minDamage, maxDamage), Enemies );
* }
}
Unity also oddly enough still claims it has the enemy in the list, as you can see I’m checking. Also, it only happens after the WaitForSeconds() function.
Lastly, the attack function:
public void Attack(int dmg, Transform obj)
{
if (called == false) {
called = true;*_

* shaker.ScreenShake ();*

* GameObject b = (GameObject)Instantiate (blood, obj.position, Quaternion.Euler (0, 0, Random.Range (-360, 360)));*

* Vector3 s1 = new Vector3 (1f, 1f, 1f);*
* Vector3 s3 = new Vector3 (1.5f, 1.5f, 1f);*

* int r = Random.Range (1, 3);*

* if (r == 1) {*
* b.transform.localScale = s1;*
* }*
* if (r == 2) {*
* b.transform.localScale = s3;*
* }*

* print (“Attack”);*

* GameObject t = (GameObject)Instantiate (attackText, obj.FindChild (“hitTextPos”).position, Quaternion.identity);*
* t.transform.FindChild (“hitText”).GetComponent ().text = dmg.ToString ();*
* Destroy (t, 2);*

* EnemyHealth hp = obj.GetComponent ();*

* hp.Hurt (dmg);*

* called = false;*
* }*
* }*
You might tell me that it’s something wrong with my WaitForSeconds() function, and you could be right, although as I’ve said this happens about 75% of the time, other times it executes the way it’s intended to and doesn’t throw up any errors.
Sorry if I’m a bit vague; I’m tired and frustrated. Ask any questions if you need to!

Well, the problem is most likely because you store the initial index of an enemy inside your list. When you remove an element from a List the indices of the following elements will change. You didn’t posted any code where you remove elements from your List but it’s the most likely case.

I don’t see a reason why you actually use the index inside your “WaitForAttack” coroutine. The IT variable already references the enemy at that index.

Also your “called” variable seems pointless. The Attack method is not a coroutine, so since Unity runs the scripting engine in a single thread it’s not possible to have multiple Attack methods run at the same time.

It might also be related to the way you actually fill your Enemies List. Since your coroutines delay the Attack action a few frames the List could be changed in the meantime,

I’ve opted to using the Find method in my list, thanks for the help though.

EDIT: It seems that the Contains method works better than Find and is simpler, it is now working without issues.