Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by zefrof · Aug 20, 2014 at 07:10 PM · c#gameobjectlist

Ordering a list of GameObjects

 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[i]);
             print (enemyList[i].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!

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Landern · Aug 20, 2014 at 07:30 PM 0
Share

What property do you want to sort on?

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Kiwasi · Aug 20, 2014 at 07:39 PM

Essentially the error is telling you there is no natural way to sort a list of GameObjects. How do you tell if which GameObject comes first?

You need to provide a sort method to GObj.Sort(). The following pseudo code sorts the GameObjects in alphabetical order by name.

 ....
 GObj.Sort(SortMethod(GameObject A, GameObjectB));
 ....

 private int SortMethod  (GameObject A, GameObjectB){
     if (!A && !B) return 0;
     else if (!A) return -1;
     else if (!B) return 1;
     else return A.name.CompareTo(B.name);
 }


Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image zefrof · Aug 20, 2014 at 08:13 PM 0
Share

Each GameObject has a script on it with a public variable speed. The greater speed means higher on the list that GameObect would be. How would I compare those values in the Sort$$anonymous$$ethod?

avatar image Kiwasi · Aug 20, 2014 at 09:59 PM 0
Share

Just change line 9 to reflect the actual comparison you want to achieve.

 private int Sort$$anonymous$$ethod  (GameObject A, GameObjectB){
     if (!A && !B) return 0;
     else if (!A) return -1;
     else if (!B) return 1;
     else if (A.GetComponent<ScriptName>().speed > B.GetComponent<ScriptName>().speed) return 1;
     else return -1;
 }


avatar image Kiwasi · Aug 20, 2014 at 10:01 PM 0
Share

If you are sorting the list frequently consider using a SortedList or SortedDictionary as these structures are more efficient.

avatar image zefrof · Aug 21, 2014 at 12:56 AM 0
Share

So I'm getting an error on

 GObj.Sort(Sort$$anonymous$$ethod(GameObject A, GameObject B));

stating unexpected symbol 'A' I'm assu$$anonymous$$g the A and B need to have a space between them and GameObject. $$anonymous$$y Sort$$anonymous$$ethod looks like this:

 private int Sort$$anonymous$$ethod (GameObject A, GameObject B){
         if (!A && !B) return 0;
         else if (!A) return -1;
         else if (!B) return 1;
         else if (A.GetComponent<Attributes>().speed > B.GetComponent<Attributes>().speed) return 1;
         else return -1;
     }


avatar image Kiwasi · Aug 21, 2014 at 01:50 AM 0
Share

I haven't ever tried to implement this, try

 GObj.Sort(Sort$$anonymous$$ethod(GameObject, GameObject));

Or

 GObj.Sort(Sort$$anonymous$$ethod);

I'll check out the docs and give you a proper syntax later

Show more comments
avatar image
1

Answer by Cherno · Aug 20, 2014 at 07:20 PM

May I direct you to this question I asked myself some time ago, it has everything you need (albeit in UnityScript):

http://answers.unity3d.com/questions/548366/sorting-an-array-of-gameobjects-by-values-inside-t.html

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Orami · Jun 15, 2016 at 06:57 AM

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;
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

26 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

What is the best way to instatiate an array of connected GameObjects? 0 Answers

Subtracting the position of transform from the position of Game Objects in a list. 1 Answer

How to select an Game Object from an Item List 0 Answers

C# List foreach problem 1 Answer

How to select the Nth gameObject in a list and put it into a gameObject variable 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges