• 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
Question by EVOin3D · Nov 17, 2013 at 06:38 AM · gameobjectlisttagaddsort

How to add and sort a list of gameobjects by tag?

I'm relatively new to using lists/programming in general, and I'm having difficulty with the following script:

 public List<GameObject> planetList = new List<GameObject>();
 
 void Update ()
 {    
     if (Physics.Raycast(ray, out hit)) 
     {
         planet = hit.collider.gameObject;
         
         if (planetList.Contains(planet) == false)
         {
             planetList.Add(planet);
         }
     }
 }

This is working fine, however each planet is being added to the list as their game object name ("Planet") I'd like to add and name each planet to the list according to their tag, for example:

 if (Physics.Raycast(ray, out hit)) 
 { 
     planet = hit.collider.gameObject;
     
     if (planetList.Contains(planet) == false) 
     {
         if (planet.tag == "Earth")
         {
             planetList.Add(planet ("Earth", 1));
         }
     }
 }

I also want to sort the list by each planet's value e.g. Earth = 1, Mars = 2. Problem is, this returns the error "cannot be used as method or delegate".

Comment

People who like this

0 Show 0
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

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by liszto · Nov 17, 2013 at 05:20 PM

Why don't use the tag value to sort your list ? I don't know what you would do but use a tag for only one object it's not the best way to use tag. ( At least for me, I prefer Enums ). So just create an enum for all your planets and sort your list with your enum values.

     if ( !planetList.Contains(planet) ) 
        if ( planet.tag.Equals( "Earth" ) )
           planetList.Add(planet ("Earth", 1)); //What would you do here ?!

You can add all your planet then when it must be over then re-sort your list based on the tag array order (or Enum order) by swapping them or by any other methods.

Comment

People who like this

0 Show 0 · 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

Answer by Bunny83 · Nov 17, 2013 at 05:25 PM

Just call sort with a custom Comparison-function

 int CompareByTag(GameObject O1, GameObject O2)
 {
     return O1.tag.CompareTo(O2.tag);
 }
 
 planetList.Sort(CompareByTag);

Btw, why don't you name your planets with their actual name? That's the purpose of a name, to distinguish similar things, like humans...

if you have a GameObject you can simply set the name, well, by setting it's name property:

 planet.name = "Betelgeuse";

If you want to sort by name, just create another Comparison function and compate the names instead.

ps: The purpose of a tag is actually to "mark" / "group" similar objects. So "Planet" would be a perfect candidate for a tag. A name on the other hand is ment to give something a unique name. It seems you use them in reverse...

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

19 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

Related Questions

Object Association in a List 1 Answer

Get GameObject from List based on attached script or assigned name 3 Answers

Problem using IComparer to sort list of GameObjects 2 Answers

sort a List of GameObjects by distance? 1 Answer

[C#] Sorting a List of Gameobjects Alphabetically 2 Answers


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