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
8
Question by Starkadder · Oct 16, 2013 at 11:19 AM · arrayordergetcomponents

Does GetComponents() return the components in the order that they are displayed in the editor?

My preliminary testing indicates the array returned by calling GetComponents() on a GameObject matches the top-to-bottom order that these components are arranged in the editor. Can anyone confirm that this is correct and/or reliable?

Thanks!

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

Answer by aldonaletto · Oct 16, 2013 at 11:55 AM

Apparently this is correct, but I would not bet my life on this! Unity may decide to change the order in future versions, since this isn't a documented feature.

Do you have multiple components of the same type attached to your game object and want to access each one independently? If so, a common solution for components added at runtime is to get a reference to each one from AddComponent - kind of:

 private var audio1: AudioSource;
 private var audio2: AudioSource;
 private var audio3: AudioSource;
 
 function Start(){
   audio1 = gameObject.AddComponent(AudioSource);
   audio2 = gameObject.AddComponent(AudioSource);
   audio3 = gameObject.AddComponent(AudioSource);
 }

Unfortunately, there's no general solution for components added in the Editor. For AudioSources (the most frequent multiple component), however, the priority field may be used as a temporary ID: you can set the first AudioSource priority to 128 in the Inspector, the second to 129, and so on; at Start, get the AudioSources in an array, identify each one and, if necessary, set their priority values to whatever they should be (the field priority is used only when there are too many AudioSources playing at the same time, and small variations around the default 128 usually don't make any difference.

Comment
Add comment · Show 4 · 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 Starkadder · Oct 16, 2013 at 12:04 PM 0
Share

Thanks for your help!

The reason I'm asking is that I want to expose some basic control over gameobjects to designers. I was considering using components, arranged in order, as a way of chaining behaviours - eg, a $$anonymous$$oveBehaviour, a WaitBehaviour, another $$anonymous$$oveBehaviour, etc.

As this doesn't look to be official or supported, looks like I can't rely on using the editor in this way. Back to the drawing board!

avatar image aldonaletto · Oct 16, 2013 at 12:16 PM 0
Share

$$anonymous$$aybe a custom editor is the best solution in your case.

avatar image Starkadder · Oct 16, 2013 at 12:41 PM 0
Share

I suspect so. Thanks again!

avatar image fafase · Oct 17, 2013 at 07:16 AM 0
Share

Damn,the priority principal is just so obvious I did not see it. I have often wondered how to get a particular AudioSource when you have many attached, but got stuck on the fact that you cannot name a component and always ended up with a children and drag and drop. Sometimes obvious just does not strike...

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

17 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

Related Questions

How to make an Array equal the Components of an Instantiated GameObject 1 Answer

Bad array declaration: Why won't this compile? 2 Answers

List.Sort with IComparer 2 Answers

Array loads in wrong order 3 Answers

Same script on multiple objects 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