Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by BryanO · May 06, 2017 at 10:47 AM · listsarray of gameobjects

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> ();
                 }
 
             }
 
         }
         }
 
 
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · 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 BryanO · May 07, 2017 at 02:10 PM 0
Share

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

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

100 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 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 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 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

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


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