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