I am trying to work out how to get the number of the maxumim depth of the heirarchy (how many levels the deepest down is), but there doesn’t appear to be anything like this in the docs or on Google, so in the hopes there is a way to do this, I am asking here.

For example:

objectParent
Child

The depth of this would be 2, as there are 2 levels.

objectParent
Child
ChildofChild

This has a depth of 3.

Is there any way to get this information from the heirarchy, or a way to work it out?

int FindDepthOfHierarchyUnder(Transform t) {
int maxDepth = 0;
for (int i = 0; i < t.childCount; i++) {
maxDepth = Mathf.Max(maxDepth, FindDepthOfHierarchyUnder(t.GetChild(i)));
}
return maxDepth + 1;
}

To get it globally, you can run it over the root gameobjects of the scene:

int FindHierarchyDepthInScene() {
var roots = SceneManager.GetActiveScene().GetRootGameObjects();
int maxDepth = 0;
foreach (var go in roots) {
maxDepth = Mathf.Max(maxDepth, FindDepthOfHierarchyUnder(go.transform));
}
return maxDepth;
}