Reputation: 397
I have this block of code. And I need to iterate through every child object, but child objects could have more children within them. The Awake()
method is called when the program is first ran. So, I'm asking how to build an iterator that will check if there are children within the children. And then run ResizeMeshCollection(MeshFilter[] collection)
on those children, until there are no more child objects.
public class ResizeAsset : MonoBehaviour {
void Awake()
{
ResizeMeshCollection(this.GetComponentsInChildren<MeshFilter>());
}
void ResizeMeshCollection(MeshFilter[] collection)
{
foreach (MeshFilter mf in collection)
{
mf.GetComponent<MeshFilter>();
Transform tf = mf.GetComponent<Transform>();
/// Resize logic here
/// check if there are children under this child object,
/// Run ResizeMeshCollection() on
/// all children until there are no more.
I'm sure that I do not want to run ResizeMeshCollection() within itself, nor do I know if that is possible, but I just can't figure out how to do this.
Upvotes: 0
Views: 741
Reputation: 8173
The way you wrote it is actually exactly how you want to do it. The method is called recursion. It is a very popular way of achieving what you are trying to do, nothing wrong with having a method calling itself, if done within certain guidelines like meeting certain criteria before executing itself again.
Upvotes: 1