- Home /
Sorting an array of GameObjects by their position
I have an array of GameObjects and I need to sort them by their position. My array is
IEnumerable platforms = GameObject.FindGameObjectsWithTag("Platform").AsEnumerable()
I have tried using
platforms = platforms.OrderBy(platform => platform.transform.position);
to sort it, but it has the error
error CS0411: The type arguments for method `System.Linq.Enumerable.OrderBy<TSource,TKey>(this
`` System.Collections.Generic.IEnumerable, System.Func)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
Is there a better way to do this?
The problem is that you're declaring platforms as non generic IEnumerable. To fix this, you can either use:
IEnumerable<GameObject> platforms = GameObject.FindGameObjectsWithTag("Platform");
platforms = platforms.OrderBy(platform => platform.transform.position);
or simply declare it as GameObject[] (you can use var keyword):
var platforms = GameObject.FindGameObjectsWithTag("Platform");
platforms = platforms.OrderBy(platform => platform.transform.position).ToArray();
EDIT: converted to comment as this solves only the specific error you described. Actual sort requires changes described in other answers.
Answer by Seregon · Nov 13, 2013 at 03:49 PM
You can only sort lists by a single value, whereas a position vector contains 3 values (x, y, z). You need to be more specific about what exactly you want to sort by, as just sorting by position doesn't make any sense, to either the computer or a human.
You could sort by position on the x-axis:
platforms = platforms.OrderBy(platform => platform.transform.position.x);
or you could sort by distance from the origin:
platforms = platforms.OrderBy(platform => platform.transform.position.sqrMagnitude);
or you could do something fancier like sorting by location on an x-z grid:
private float GridRanking(Vector3 Pos)
{
return pos.x + 100 * pos.z;
}
platforms = platforms.OrderBy(platform => GridRanking(platform.transform.position));
The above code is an example only, and should sort items into cells of 1x1 unit on the horizontal plane, so long as no item has an x-coord above 100 (increase the multiplier if it has). It also won't work for negative coordinates.
If you provide more details about what it is your trying to achieve, I can try and produce some better code.
Sorry for not being clear with what I wanted to do - I want to sort the array by the relative position to the GameObject that this script is attached to, closest first.
Ah, in that case the code would look something like this:
platforms = platforms.OrderBy(platform => (platform.transform.position - transform.position).sqr$$anonymous$$agnitude);
Note that ArkaneX's comment about using IEnumerable may also be correct, and I've not had a chance to test this code.
i think it can will solve your problem:
platforms= platforms.OrderBy(obj => (obj.transform.position this.transform.position).sqr$$anonymous$$agnitude).ToArray();
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
[C#] Sorting a List of Gameobjects Alphabetically 2 Answers
C# copy Sorting data of An Array to Another Array 1 Answer
Instantiate a prefab from jsonarray? 1 Answer