Hello human beings.
I’m tring to find the “clothes” tag on a child of the gameobject running the script.
CODE:
Color m_color = new Color (red, green, blue);
foreach (Transform child in transform) {
if (child.CompareTag ("Clothes")) {
print (child);
print (m_color);
GetComponent<Renderer> ().material.color = m_color;
My code doesn’t print anything.
Help pls.
Ethan
To iterate all childs (not only immediate childs) you can use recursive method:
public GameObject MyGameObject;
public string TagToFind;
private void Start()
{
GameObject myChild = GetChildWithTag(MyGameObject, TagToFind);
if (myChild != null)
{
Debug.Log(string.Format("Found child with tag {0}", TagToFind));
}
else
{
Debug.Log(string.Format("There is no child with tag {0}", TagToFind));
}
}
private GameObject GetChildWithTag(GameObject gameObject, string tagToFind)
{
foreach (Transform child in gameObject.transform)
{
if (child.gameObject.CompareTag(tagToFind))
{
return child.gameObject;
}
if (child.childCount > 0)
{
return GetChildWithTag(child.gameObject, tagToFind);
}
}
return null;
}