I got my game object create like the image below :

My code is compile in PanelStatus and I wish to control the object UnitImage.
But while I search from child I could only get the first layer which means only the PanelUnit have been returned.
Here is my code :
Image UnitImageImage;
const string UnitImageObjectName = "UnitImage";
void Awake()
{
foreach(Transform child in transform)
{
switch (child.name)
{
case UnitImageObjectName:
UnitImageImage = child.GetComponent<Image>();
break;
default:
Debug.Log(child);
break;
}
}
}
You can use Unity’s inbuild method ‘GetComponentsInChildren’:
private GameObject GetChildByName(GameObject parent, string childName, StringComparison stringComparison = StringComparison.InvariantCulture)
{
return parent.GetComponentsInChildren<Transform>()
.Where(a => a.gameObject.GetInstanceID() != parent.GetInstanceID() &&
a.name.Equals(childName, stringComparison))
.Select(a => a.gameObject)
.FirstOrDefault();
}
private GameObject[] GetChildrenByName(GameObject parent, string childName, StringComparison stringComparison = StringComparison.InvariantCulture)
{
return parent.GetComponentsInChildren<Transform>()
.Where(a => a.gameObject.GetInstanceID() != parent.GetInstanceID() &&
a.name.Equals(childName, stringComparison))
.Select(a => a.gameObject)
.ToArray();
}