Question about making a transform array of grandchildren

So on the inspector I made a parent, child, and grandchild, so I can sort my grandchildren all while having them in the same parent folder. What I want is to make a list of all the grandchildren of characterFolderChildren (not the parents, children, or children + of the grandchildren… so ONLY the grandchildren), so I can call a function on them later. I made a transform list currently, and it works, but I have to use the same code twice, first to get the length that the list will be, and then a second time to actually add the transforms to the list (since ListName = new Transform[Length]; resets the transforms in the list when called). What I am asking is, is there a shorter way to make this so I can make a list of all the grandchildren? The more grandchildren I make, the more I would have to hardcode the length, so I made a longer length for the moment, but I will still probably have to update that…

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CharacterItemInitializationManager : MonoBehaviour
{
    public GameObject CharacterFolder;

    public Transform[] characterFolderChildren;
    public Transform[] allGrandChildren;

    public int grandchildCounter;
    // Start is called before the first frame update
    void Start()
    {
        characterFolderChildren = new Transform[CharacterFolder.transform.childCount]; //This is the total number of transforms in the array, and it also removes all current transforms in array
        for (int i = 1; i <= CharacterFolder.transform.childCount; i ++)
        {
            characterFolderChildren[i-1] = CharacterFolder.transform.GetChild(i-1);
        }
        allGrandChildren = new Transform[20]; //Set this extra long so you can add characters without a problem
        grandchildCounter = 0;
        for (int i = 1; i <= CharacterFolder.transform.childCount; i++)
        {
            for (int j = 0; j <= 1; j++)
            {
                grandchildCounter += 1; //This counts the number of grandchildren so the new Transform 4 lines below knows how long it should be (the initial length is set extra long)
            }
        }
        allGrandChildren = new Transform[grandchildCounter];
        for (int i = 1; i <= CharacterFolder.transform.childCount; i++)
        {
            for (int j = 0; j <= 1; j++)
            {
                allGrandChildren[j] = CharacterFolder.transform.GetChild(i - 1).GetChild(j);
            }
        }
    }
}

Thanks

Nevermind, fixed… I don’t know how to delete posts…

Don’t delete posts, instead, tell us all how you fixed it!

Improve the world…

1 Like

If you want a collection like an array that you can dynamically resize, use List with Add() instead. It’ll save that middle step.

2 Likes

Just use something like that:

using UnityEngine;
using System.Collections.Generic;
using System.Linq;

public static class TransformExt
{
    public static List<Transform> GetGrandChildren(this Transform aTrans, List<Transform> aResults = null)
    {
        if (aResults == null)
            aResults = new List<Transform>();
        foreach(Transform t in aTrans)
        {
            aResults.AddRange(t.Cast<Transform>());
        }
        return aResults;
    }
}

With this extension method you can simply use it like this:

var grandChilds = yourTransform.GetGrandChildren();
foreach(Transform gChild in grandChilds)
{
    // do something
}

Unfortunately I found a work around to the entire system so I didn’t need this in the first place.