- Home /
Confusion over LINQ syntax
I'm trying to use LINQ's OrderBy to organize a list by distance, such that the following function will always organize myList[0] to be the closest gameobject to myObject:
void SortObjectsByDistance(List<GameObject> myList, GameObject myObject){
myList = (List<GameObject>)myList.OrderBy (myObject => (myObject.transform.position - transform.position).sqrMagnitude);
}
However, this produces an error which makes me think my syntax is off: "a local variable named 'myObject' cannot be declared in this scope because it would give a different meaning to 'myObject', which is already used in a parent or current scope to denote something else". There's nothing called myObject outside of this function, which makes me think I'm using LINQ wrong- do I need to make a third, junk variable and assign it to myObject somehow?
Answer by fafase · Nov 24, 2014 at 07:24 AM
myList = myList
.OrderBy(myObject=>Vector3.Distance(myObject.transform.position,transform.position)
.ToList();
EDIT: I removed one set of () around the parameter of Distance as there were two sets. Her below is a demo program that is tested and working:
List<GameObject> myList = new List<GameObject>();
void Start ()
{
GameObject objA = new GameObject();
objA.transform.position = new Vector3(5,0,0);
GameObject objB = new GameObject();
objB.transform.position = new Vector3(2,0,0);
GameObject objC = new GameObject();
objC.transform.position = new Vector3(7,0,0);
GameObject objD = new GameObject();
objD.transform.position = new Vector3(6,0,0);
GameObject objE = new GameObject();
objE.transform.position = new Vector3(1,0,0);
myList = new List<GameObject>(){objA, objB, objC, objD, objE };
myList = myList.OrderBy(myObject => Vector3.Distance(myObject.transform.position,transform.position)).ToList();
}
Hmm, I might be doing something wrong, but replacing the contents of my function with this produces three errors- the original "give a different meaning to myObject" one, an unexpected ',' on the .OrderBy line, and an internal compiler error during parsing.
You had a myObject as parameter, which was the different meaning error. The myObject within the OrderBy is created by the OrderBy method. This is a little tricky to understant but what you are doing is that you tell the compiler to fetch an item from the list and name it myObject periodically, then use it in the Distance method against another variable that in this case is the object holding the script.
Just like when you use foreach:
foreach (GameObject myObject in myList){
// iterate through myList, each item is called myObject
// inside the foreach loop
float distance = Vector3.Distance(myObject.transform.position,
transform.position);
// $$anonymous$$ore sorting here
}
That works!! Thank you very much- my problem was misunderstanding how OrderBy worked, I thought myObject was something you passed to it that it compared against the item in the list, not the holding value as you moved the list around. :)
Answer by tanoshimi · Nov 24, 2014 at 07:45 AM
"There's nothing called myObject outside of this function". No, but there is already a (unnecessary) local parameter called myObject specified in the function signature, which is accessible from within the lambda's scope and therefore conflicts with the declaration of myObject there.
I also think you'll have problems with your approach of casting the result back to your List. Try this instead:
void SortObjectsByDistance(List<GameObject> myList){
myList = myList.OrderBy(myObject => (myObject.transform.position - transform.position).sqrMagnitude).ToList();
}
This compiles, but I'm a little confused- if I'm not feeding it myObject when the function runs, how and where would I specify which object myObject holds a reference to?