This error is being thrown when running a Linq query over a List.
I am using Unity3D 3.0 with C# (Unity3D uses Mono 2.6). Unity3D, as far as I know, is single-threaded. It works by attaching "scripts" (c# .cs files) that inherit a baseclass, to a "GameObject". Also, Unity controls instantiation and serialization of scripts so you can't use constructors.
I have a RoadNetwork script that holds a reference to RoadNodes and RoadCurves, both of which locate RoadNetwork via a singleton and register/deregister themselves. I've put "mini-factories" in RoadNode and RoadCurve that do the hard work of hooking themselves up to a gameobject.
RoadNode first checks with RoadNetwork to make sure there isnt already a node at that same position, by doing this:
public static RoadNode2 New(float x, float y, float z)
{
//First try to find an existing one
var rn = RoadNetwork.Instance.GetNodeAtPosition(new Vector3(x, y, z))
?? UnityReferenceHelper.GetNewGameObjectFor<RoadNode2>(
"RoadNode_" + (RoadNetwork.Instance.Nodes.Count + 1).ToString("D3"),
RoadNetwork.Instance.transform.FindChild("Nodes"));
rn.Position = new Vector3(x, y, z);
rn.gameObject.active = true;
return rn;
}
Where the appropriate method in RoadNetwork is:
public RoadNode2 GetNodeAtPosition(Vector3 position)
{
var tempList = new List<RoadNode2>();
return tempList.Single(x => x.Position == position);
}
tempList was an attempt at narrowing down the problem but I get precisely the same error. It should be "Nodes.Single(...", but I doubt it matters. I get the same error if I call the Linq query directly in the New() method.
So yes, this Exception throws and points me to that tempList.Single() line. What would the reason be?