• 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
4
Question by Mediahaze · Apr 27, 2010 at 02:35 AM · tagsfindgameobjectswithtaginitialization

Order of GameObject.FindGameObjectsWithTag(string tag)

Hello, I am trying to discover the ordering of the objects in the array returned by GameObject.FindGameObjectsWithTag().

Is the order something that I can depend on such as being based on the name of the object? Or is it some other property or defined by the initialization time of the object?

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

3 Replies

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

Answer by duck · Apr 27, 2010 at 01:23 PM

The ordering of most of unity's commands which return lists of objects is not specified (i.e. it is undocumented). This includes the arrays returned by:

  • FindGameObjectsWithTag
  • FindObjectsOfType
  • GetComponents
  • GetComponentsInChildren

and the ordering of the child nodes when Iterating through a transform's children.

For this reason, you shouldn't ever assume that objects will always occur in a certain order, or following a certain pattern from these functions, even if they seem to do so in your build. For example, an update to the webplayer could potentially change how these functions work "under the hood", resulting in a different ordering, after you deployed your webplayer!

So, the upshot is that you need to design your systems in such a way that does not rely on the ordering of these results.

If you need to collect objects in a certain order (for example a series of waypoints around a circuit in your scene), I would advise naming them numerically, and finding them by name at the start of your script, as described here.

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
7
Best Answer

Answer by Molix · Apr 27, 2010 at 03:49 AM

I just did a quick test and name is definitely not the determining factor for the order.

Observationally I'd guess there's an internal array built at startup for each tag, so it is probably filled in based on the order the objects were added to the scene, then at runtime if there are any changes to tags they seem to be just added to the end of that list. That's just based on a few minutes of testing though, so don't quote me.

If you need them to be sorted by name, you can do that easily enough, e.g. (C#):

GameObject[] FindObsWithTag( string tag ) { GameObject[] foundObs = GameObject.FindGameObjectsWithTag(tag); Array.Sort( foundObs, CompareObNames ); return foundObs; }

int CompareObNames( GameObject x, GameObject y ) { return x.name.CompareTo( y.name ); }

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 Mediahaze · Apr 27, 2010 at 03:52 PM 0
Share

I used this code to solve my problem. Thank you.

avatar image
3

Answer by Eric5h5 · Apr 27, 2010 at 04:16 AM

The order is undefined, so you can't count on it in any way. Even if you find something that works now, it can change in the future, so you have to use some defined way of ordering the objects (sorting by name or whatever).

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 AdityaViaxor · Apr 05, 2019 at 06:14 AM 0
Share

Thank you very much it worked very well. saved my time thank you

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

GameObject.FindGameObjectsWithTag problems 2 Answers

Exclude my gameObject tag from FindGameObjectsWithTag 2 Answers

Accessing all GameObjects with a certain tag 1 Answer

Initialising List array for use in a custom Editor 1 Answer

How to find one object in specific layer? 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