- Home /
Hiding/Revealing UI elements in Children
I'm attempting to make renderers 3 levels down the hierarchy from my base Unit gameObject flip on and off so that only the currently selected unit's data is showing up in the corner of the screen. I saw a solution a few years old that suggested I could dig into the hierarchy with a basic "(objectName/child/child)" organization, but that hasn't worked in practice - possibly because Unity has changed or possibly because of the types I'm trying to have as children. I can't grab ALL the renderers on the object because I want, for instance, the Unit model to remain visible. I just want parts of its UI to become visible/invisible.
I'm hoping to go through a List of Units in a for loop, like
for (int i = 0; i < initList.Count; i++)
{
if (initList[i] == currentUnit)
{
//activate the renderers
}
else
{
//deactivate the renderers
gameObject.Find(initList[i]/Canvas/CharacterInfo/Text).Text.enabled = false;
}
}
But the above Find doesn't compile ("'Canvas' is a type, which is not valid in the current context").
Answer by petur · Sep 10, 2019 at 10:52 PM
You can look for the type of component you need with GetComponentsInChildren<Text>()
A more performant solution is to keep references to the things you need to activate and deactivate in the script and assign them in the inspector.
If you don't want to assign them manual you can give the gameobject you want to turn on and off an specific name and check the hierarchy downwards for it at the awake function.