There are at very least 4 options: [`GetChild`](http://answers.unity3d.com/questions/30296/question-regarding-transformgetchild.html), [`GetComponentsInChildren`](http://docs.unity3d.com/Documentation/ScriptReference/GameObject.GetComponentsInChildren.html), [`FindObjectsOfType`](http://docs.unity3d.com/Documentation/ScriptReference/Object.FindObjectsOfType.html?from=GameObject) and [`FindGameObjectsWithTag`](http://docs.unity3d.com/Documentation/ScriptReference/GameObject.FindGameObjectsWithTag.html). We could also use some variations that won't bring an array, for instance. Listed below in no particular order.
1. **GetChild** a method of `Transform`, it still works even today on Unity4, and it's still not in the Docs for whatever reason. It will bring the child be it active or not. Probably the cheapest to use, since we have to use in each immediate parent we want to get the inactive GameObject. It will just be a pain to code and maintain.
1. **GetComponentsInChildren** needs to have a root object. We could set up the scene with just 1 main root here. Very simple to use, but also very heavy. Apply this in the root object:
foreach (Transform child in GetComponentsInChildren(true))`
1. **FindObjectsOfType** doesn't need a root object. Docs say it won't return inactive objects, but it does. Since it's the heaviest listed here and highly unadvised to use, we could do something like:
foreach ( GameObject root in GameObject.FindObjectsOfType(typeof(GameObject)) ) {
if (root.transform.parent == null) { // game object with no parent
// here, iterate through each `root` child using your prefered method
// or simply remove the 'if' above
}
}
1. **FindGameObjectsWithTag** my favourite [one][1], way faster and simpler than all others, but we can't add more than 1 tag per object so it may be prohibitive if we already use tag for something else. It also needs no root, use it from anywhere:
foreach ( GameObject obj in GameObject.FindGameObjectsWithTag("tag") )
Disclaimer: @bdawson actually gave the same solution about FindObjectsOfType and @Antony scratched it, twice. None were specific enough.
[1]: http://answers.unity3d.com/questions/48252/how-to-find-inactive-gameobject.html?page=2#answer-320092
↧