Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
1
Question by Wampster · Nov 24, 2016 at 08:18 AM · getcomponentinchildren

How can I access GetComponentInChildren as string?

I want to access two Sliders on the same GameObject. Usually, I'd just use:

 mySlider = GameObject.Find("myObject").GetComponentInChildren<Slider>();

However, I want to access one slider for music and one for soundFX. To distinguish the two, I thought that I'd try this method:

 musicSlider = GameObject.Find("HUDCanvas").GetComponentInChildren("MusicVolume") as Slider;
 musicToggle = musicSlider.GetComponent<Toggle>();
 soundSlider= GameObject.Find("HUDCanvas").GetComponentInChildren("SoundFXVolume") as Slider;

It seems that we can't use a string for "InChildren".

The only other way that I can think of is to create an array, then loops through the array to find component by string. Is there a better solution or is this how you'd do it?

Comment
Add comment · Show 5
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 Mister-Mortal · Nov 24, 2016 at 08:28 AM 0
Share

GetComponentInChildren() uses string name of a class as a parameter, so you can't pass object names to it, it will find nothing.

avatar image 5c4r3cr0w · Nov 24, 2016 at 08:53 AM 0
Share

Try this:

 myslider = GameObject.Find ("$$anonymous$$yObject").transform.GetChild (<index Of Slider>).GetComponentInChildren<Slider>() as Slider;
 
avatar image hexagonius 5c4r3cr0w · Nov 24, 2016 at 09:32 AM 0
Share

"as Slider" is not needed, the generic already does this

avatar image hexagonius · Nov 24, 2016 at 09:31 AM 0
Share

there is GetComponentsInChildren, which returns an array of all components in order. since you know the order you can just access the result by index

avatar image Wampster · Nov 24, 2016 at 10:34 AM 0
Share

I didn't know that we could simply call an index in that way. That is good knowledge. Thank you.

1 Reply

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

Answer by LK84 · Nov 24, 2016 at 09:57 AM

You can write an extension method gor game object to find a component in a child by name (or by tag which is faster). It could look like this:

     public static T FindComponentInChildWithTag<T>(this GameObject parent, string tag) where T : Component
         {
             Transform t = parent.transform;
             foreach (Transform tr in t)
             {
                 if (tr.tag == tag)
                 {
                        if(tr.GetComponent<T>!=null)                 
                              return tr.GetComponent<T>();
                 }
                 else //Find in GrandChildren
                 {
                     T dummy;
                     dummy = tr.gameObject.FindComponentInChildWithTag<T>(tag);
                     if (dummy!=null)
                         return dummy;
                 }
             }
     
             return null;
         }
 

Note: It's generic, so you can use it for any component, not only slider. Plus it also searches for grandchildren. It you don't need that stuff, you can remove it of course. You can use the extension method like this:

 musicSlider=GameObject.Find("myObject").FindComponentInChildWithTag<Slider>("MusicVolume");



Comment
Add comment · Show 3 · 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 Wampster · Nov 24, 2016 at 10:37 AM 0
Share

This is a nice solution. $$anonymous$$uch more advanced than anything that I would have thought of. I am going tp have to go and digest this. Thank you very much for your help.

avatar image LK84 Wampster · Nov 24, 2016 at 11:37 AM 0
Share

You're welcome. I just added a little modification to check if the gameobject with the right tag has the component (otherwise you'd get null returned). Forget that when I wrote the reply first

avatar image brian-nielsen · Dec 04, 2020 at 05:57 PM 0
Share

4 years later this is still helping. Thank you very much :)

For those like me that didn't know what an extension method was here is a little Unity video that explains.

https://learn.unity.com/tutorial/extension-methods#5c89416dedbc2a1410355318

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

8 People are following this question.

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

Related Questions

GetComponentInChildren question 1 Answer

Is GetComponentsInChildren What I'm Looking For 1 Answer

GetComponentInChildren< Renderer > failing to find the renderer 1 Answer

How to use GetComponentsInChildren? (C#) 1 Answer

Trying to get transform in child object 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