Search Unity

C# Array sorting horror - SOLVED :)

Discussion in 'Scripting' started by psychicparrot, May 31, 2009.

  1. psychicparrot

    psychicparrot

    Joined:
    Dec 10, 2007
    Posts:
    884
    Hey guys,

    I could really do with some help on this code. I want to iterate through my game world, grab all of the GOs whose name contains "_monster", throw them into an array then sort the array alphabetically. This is what I have so far...

    Code (csharp):
    1.         UnityEngine.Object[] gameobjects = FindObjectsOfType(typeof(GameObject));
    2.         monsters=new ArrayList();
    3.        
    4.         foreach(GameObject theGO in gameobjects){
    5.             string str = theGO.name;
    6.             int pos = str.IndexOf("_monster",0);
    7.             if(pos>0){
    8.                 //found a monster
    9.                 monsters.Add(theGO.gameObject);
    10.             }
    11.         }
    12.        
    13.         GameObject[] monsterObjects = (GameObject[]) monsters.ToArray(typeof(GameObject));
    14.         Array.Sort(monsterObjects);

    Which works right up until the Array.Sort line, where Unity throws the error ...

    InvalidCastException: Cannot cast from source type to destination type.
    System.Array.Sort (System.Array array)

    Any ideas how to solve this? It's a really hideous long winded method of doing this I know ... any shorter solutions would also be totally welcomed, but as long as it works I will be happy ... performance isn't a huge issue as I'm doing this during an initialization stage... I just need it to work :)

    Thanks all!
     
  2. Kai

    Kai

    Joined:
    May 28, 2009
    Posts:
    24
    I don't know if the Mono version that Unity uses has access to the Sorted Dictionary, but thats what I would use.

    http://msdn.microsoft.com/en-us/library/f7fta44c.aspx

    You can store your game objects by using their name as the key and the game object itself as the value.

    That error you're getting is probably because Mono doesn't know how to sort GameObject.
     
  3. raleighr3

    raleighr3

    Joined:
    Jul 9, 2008
    Posts:
    106
    Would be better to add a custom tag to all your monster objects and use
    GameObject.FindGameObjectsWithTag("tag");

    the you could do this:

    Code (csharp):
    1. // Need to know how to sort monsters, Implement a Comparer
    2. public class myMonsterSorter : IComparer  {
    3.  
    4.       // Calls CaseInsensitiveComparer.Compare on the monster name string.
    5.       int IComparer.Compare( System.Object x, System.Object y )  {
    6.       return( (new CaseInsensitiveComparer()).Compare( ((GameObject)x).name, ((GameObject)y).name) );
    7.       }
    8.  
    9.    }
    10.  
    11. //do the sort
    12. void Start()
    13. {
    14.      IComparer myComparer = new myMonsterSorter();
    15.      GameObject[] gameobjects = GameObject.FindGameObjectsWithTag("aMonster");
    16.     Array.Sort(gameobjects, myComparer);
    17.     foreach (UnityEngine.Object obj in gameobjects)
    18.     {
    19.         Debug.Log("obj = " + obj.name);
    20.     }
    21. }
     
    Lohaz and Jean-Fabre like this.
  4. psychicparrot

    psychicparrot

    Joined:
    Dec 10, 2007
    Posts:
    884
    Thanks, guys :)

    I ended up with the monsterSorter:

    Code (csharp):
    1.         IComparer myComparer = new myMonsterSorter();
    2.         Array.Sort(monsterObjects, myComparer);
    I really appreciate the help - that was, like a steering wheel down the trousers, driving me nuts ;)

    AWESOME! :D