Collection was modified; enumeration operation may not compute
The following is a script for my enemies in a 2D game that looks at certain points. The majority of the time it works, although every one in a while (1/5ish) the error. I can't find any way to replicate the error consistently, sometimes it throws once every time the coroutine runs, sometimes it throws multiple times. 99% sure the error isn't due to the layermasks or tagging, 100% sure not do the allWayPoints list. It is a little complex but any help is welcome.
InvalidOperationException: Collection was modified; enumeration operation may not execute
It says the error is at line 778 in the file List.cs.
//Needs work (buggy and glitchy sometimes)
IEnumerator lookAround(float time)
{
visiblePoints.Clear();
//Raycasting and adding to visiblePoints list
foreach (GameObject obj in allWayPoints)
{
RaycastHit2D hit = Physics2D.Raycast(transform.position, obj.transform.position - transform.position, Mathf.Infinity, layerMask);
if (hit.collider.tag == "LookPoints")
{
float dirAngle = Vector3.Angle(transform.right, (hit.transform.position - transform.position).normalized);
Vector3 cross = Vector3.Cross(transform.right, (hit.transform.position - transform.position).normalized);
//Checks which direction of angle
if (cross.z < 0)
dirAngle = -dirAngle + 360;
Waypoints newPoint = new Waypoints(obj.transform.position - transform.position, dirAngle, hit.distance);
bool checker = false;
//Makes sure visible points aren't too similar of an angle
foreach (Waypoints item in visiblePoints)
{
if (Mathf.Abs(dirAngle - item.eulerZ) < 20)
checker = true;
}
if (!checker)
visiblePoints.Add(newPoint);
}
}
//Sorting
visiblePoints.Sort();
//Reversing if necessary
if (360 - visiblePoints[visiblePoints.Count - 1].eulerZ < visiblePoints[0].eulerZ)
visiblePoints.Reverse();
//Looking at each point
while (timer < time)
{
//So it runs at least twice
for(int i = 0; i < 2; i++)
{
foreach (Waypoints item in visiblePoints)
{
//Rotates to look at angle
while (Vector3.Angle(transform.right, item.direction) > 3)
{
transform.right = Vector3.RotateTowards(transform.right, item.direction, Time.deltaTime * rotSpeed, 0);
yield return null;
}
yield return new WaitForSeconds(1.1f);
}
}
}
//Reseting
visiblePoints.Clear();
lastKnownPos = resetPos;
lastSuspectPos = resetPos;
wasExecuted = false;
}
Class code:
public class Waypoints : IComparable<Waypoints>
{
public Vector3 direction;
public float eulerZ;
public float distance;
//Gives class ability to be sorted
public int CompareTo(Waypoints other)
{
if (this.eulerZ > other.eulerZ)
return 1;
else if (this.eulerZ < other.eulerZ)
return -1;
else
return 0;
}
//Constructor
public Waypoints(Vector3 dir, float z, float dis)
{
direction = dir;
eulerZ = z;
distance = dis;
}
}
I guess I'm kind of asking a lot here. Any help is great, as I don't fully understand lists or coroutines, which I suspect to the problem somehow. I'm really lost, the error is so inconsistent and I have no idea what is going on, or what the List.cs file is.
Any questions just ask, I probably forgot some important info.
What line exactly is the error? I realize you said it was line 778. However, We see 1 through 68 here.
The error is in List.cs. I'm pretty sure the List.cs file is the code included for lists, I didn't write it myself. This code here is in EnemyAI script which is only about 250 lines.
I think how it works is there are script for ints, floats, strings, etc. I believe List.cs is just another one of these. Not gonna lie don't really fully understand what is going on.