Question by
SpaceDwarf · Jul 14, 2016 at 04:51 PM ·
c#unity 5errorforeach
InvalidOperationException: Collection was modified; enumeration operation may not execute.
I'm getting the error InvalidOperationException: Collection was modified; enumeration operation may not execute. Apparently this is to do with my foreach loop, but I can't see any other way to perform this practically.
public class point
{
public string name;
public Vector3 position;
public point(string newName, Vector3 newPos)
{
name = newName;
position = newPos;
}
}
public class Linehandle : MonoBehaviour {
//public GameObject[] allcities;
public GameObject[] othercities;
public GameObject closest;
public Vector3 currentpos;
public Vector3 close;
// Use this for initialization
void Start () {
List<point> allcities = new List<point>();
List<point> othercities = new List<point>();
currentpos = new Vector3 (-3, 1, 0);
allcities.Add (new point ("city1", currentpos));
currentpos = new Vector3 (1, 1, 1);
allcities.Add (new point ("city2", currentpos));
foreach(point city in allcities){
othercities = allcities;
othercities.Remove(city);
foreach(point other in othercities)
{
Debug.DrawLine (other.position,city.position);
}
}
}
Comment
Your answer
Follow this Question
Related Questions
InvalidOperationException: Collection was modified; enumeration operation may not execute 1 Answer
Missing the class attribute 'ExtensionOfNativeClass' 4 Answers
Error in Inventory Script 1 Answer
The name does not exist in the current context 3 Answers
InvalidCastException: Cannot cast from source type to destination type. 1 Answer