Skip to content

Instantly share code, notes, and snippets.

@dbuck
Created August 16, 2011 15:04
Show Gist options
  • Select an option

  • Save dbuck/1149310 to your computer and use it in GitHub Desktop.

Select an option

Save dbuck/1149310 to your computer and use it in GitHub Desktop.
Return the full hierarchy string of a gameObject, ready for use in GameObject.Find method later
//Return the full hierarchy string of a gameObject, ready for use in GameObject.Find method later...
static public string GetHierarchy(GameObject g)
{
if (!g || g == null)
{
return "";
}
string hierarchy = null;
string gname = g.name;
Transform parent = g.transform.parent;
while (parent)
{
hierarchy += "/" + parent.gameObject.name;
parent = parent.parent;
}
string inverse = gname + hierarchy;
string[] arrayh = (string[])inverse.Split(new string[] { "/" }, StringSplitOptions.None);
Array.Reverse(arrayh);
return "/" + string.Join("/", arrayh);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment