Skip to content

Instantly share code, notes, and snippets.

@leventeren
Created October 6, 2020 20:24
Show Gist options
  • Save leventeren/edfe6810f61323d0acfaa2e000ee0e7b to your computer and use it in GitHub Desktop.
Save leventeren/edfe6810f61323d0acfaa2e000ee0e7b to your computer and use it in GitHub Desktop.
public static GameObject FindChild(Transform trans , string childName)
{
Transform child = trans.Find(childName);
if (child != null)
{
return child.gameObject;
}
int count = trans.childCount;
GameObject go = null;
for(int i = 0 ; i < count ; ++i)
{
child = trans.GetChild(i);
go = FindChild(child, childName);
if (go != null)
return go;
}
return null;
}
public static T FindChild<T>(Transform trans, string childName) where T : Component
{
GameObject go = FindChild(trans,childName);
if(go == null)
return null;
return go.GetComponent<T>();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment