using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class GoList : MonoBehaviour {
public GameObject[] enemyList;
public int enemyListLength;
// Use this for initialization
void Awake () {
enemyList = GameObject.FindGameObjectsWithTag ("Enemy");
enemyListLength = enemyList.Length;
List<GameObject> GObj = new List<GameObject> ();
for (int i = 0; i < enemyListLength; i++){
GObj.Add (enemyList*);*
_ print (enemyList*.name);_
_ }*_
* GObj.Sort ();* * }*
* // Update is called once per frame* * void Update () {*
* }* } When I run this code I get an error (ArgumentException: does not implement right interface) on the line with sort. If I comment that line everything works great, except for the fact that the list isn’t sorted. I want to be able to sort the list based off a variable stored in the GameObjects. Some research I’ve done says the icomparer/icomparable might help, but I have no idea how to implement that. I’d prefer no to use Linq if I can get away with it, it still confuses the heck out of me haha. Any and all help will be appreciated!
I think you can write an inherited class from IComparer to tell the code how to sort your list. I believe this code works - haven’t gotten to multiplayer testing yet so it might need some tweaking.
using System.Collections.Generic;
public class AggroComparer : IComparer<AggroData>
{
public int Compare(AggroData x, AggroData y)
{
if (x.AggroLevel == y.AggroLevel)
{
return 0;
}
if (y.AggroLevel > x.AggroLevel)
{
return 1;
}
else return -1;
}
}