How do I sort GameObjects [] gos ? in C#
C# ANSWER ONLY PLEASE: How do I sort GameObjects [] ? Below is my hack for what I did - but I want to know what are people doing to sort GameObject [] lists?
//using free plugin multitags https://www.assetstore.unity3d.com/en/#!/content/21475
public void FindClosestEnemy ()
{
if (m_Enemy == null){
GameObject [] gos = MultiTags.FindGameObjectsWithMultiTag("Entity");
//I put this next tag sort becasue this is what I wanted //GameObject [] myTeamates = MultiTags.FindGameObjectsWithMultiTag(myTag);
//But this is what I did
List<GameObject> list = new List<GameObject>(gos);
for (int i = 0; i + 1 < gos.Length; i++) {
if (gos [i].HasTag (myTag)) {
list.Remove(list[i]);
}
}
gos = list.ToArray();
closestEnemy = null;
float distance = Mathf.Infinity;
Vector3 position = transform.position;
foreach (GameObject go in gos) {
if (go == gameObject) continue;
Vector3 diff = go.transform.position - position;
float curDistance = diff.sqrMagnitude;
if (curDistance < distance) {
closestEnemy = go;
distance = curDistance;
}
if (closestEnemy.tag ==("NPC")) {
// m_Enemy = null;
// return;
m_Enemy = closestEnemy.GetComponent<NPCScript> ();
}
if (closestEnemy.tag== ("Player")) {
// m_Enemy = null;
// return;
m_Enemy = closestEnemy.GetComponent<PlayerControlBase> ();
}
}
}
}
Answer by NoseKills · May 07, 2017 at 10:47 AM
The first question to ask is: do you really need to sort the entire array? In a basic case, sorting the whole array requires comparing each element in it to every other element in the array, resulting in running your comparison check array.Length-1 * array.Length-1
times and reading and re-writing elements in the array to their new positions. That's a lot of operations.
If you just need to find out which item in the array is the "most suitable" one for your purposes, you should keep the code you are using now. This code only has to compare the previously found best option against each remaining array element once, which is an order of magnitude less operations than sorting tye whole array.
If you really need to sort the array, use some overload of Array.Sort for example. So make a function that takes in object A and object B and returns a negative number if A should come first in the array and a positive number if B should come first and use that as the Comparison method in Array.Sort.
Perhaps I need to ask this question a different way.
The enemies are attacking members of their own $$anonymous$$m - this code I am using here doesn't seem to be excluding the objects own tag from a sort. Or perhaps Im not casting/recasting the built in array correctly during my check for the tag. Please respond with specific code thanks.
public void FindClosestEnemy ()
{
if (m_Enemy == null){
GameObject [] gos = $$anonymous$$ultiTags.FindGameObjectsWith$$anonymous$$ultiTag("Entity");
List list = new List(gos);
for (int i = 0; i + 1 < gos.Length; i++) {
if (gos [i].HasTag (myTag)) {
list.Remove(list[i]);
}
}
gos = list.ToArray();
Your answer
Follow this Question
Related Questions
Merging multiple array's from a specific class without manually targeting them 0 Answers
Convert a list into a GameObject[] Array,How to convert a list to a gameObject[] 1 Answer
List wont appropriately add objects. 1 Answer
Store instantiated object in List 2 Answers
Creating card game - copy of list to new list - creating cycle 1 Answer