- Home /
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?
GetComponentInChildren() uses string name of a class as a parameter, so you can't pass object names to it, it will find nothing.
Try this:
myslider = GameObject.Find ("$$anonymous$$yObject").transform.GetChild (<index Of Slider>).GetComponentInChildren<Slider>() as Slider;
"as Slider" is not needed, the generic already does this
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
I didn't know that we could simply call an index in that way. That is good knowledge. Thank you.
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");
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.
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
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

Follow this Question
Related Questions
transform.root and empty root game objects don't play nice 1 Answer
Text Not Being Displayed above Instantiated Prefabs 0 Answers
GetCompontentInChildren erratic behaviour 0 Answers
how do I change the properties of a material that are on the children of an object on script? 1 Answer
Get sphere at run time 1 Answer