All I’m trying to do is to get all the Child Transforms that are present within a target and return them as an array. I have done and followed examples online, including the API, and I get errors.
This is how I have it right now. This gets the return method of the Transforms
//get all children on target object
Transform[] _children = (Transform[])_target.GetChildrenOfObject();
Don’t use GetComponentsInChildren because it does exactly what you have described. It includes the parent as well as all component in any children, no matter how deeply nested.
The Transform class implements the IEnumerable interface. Unfortunately only the type-less version. Though a foreach loop can handle that directly:
foreach(Transform child in transform)
{
// ...
}
The second way, if you want the childs in an array or list, is to use Linq.
// of course at the top
using System.Linq;
// as typed enumerable
IEnumerable<Transform> childs1 = transform.Cast<Transform>();
// as array
Transform[] childs2 = transform.Cast<Transform>().ToArray();
// as list
List<Transform> childs3 = transform.Cast<Transform>().ToList();
The third way, without using Linq would be to use childCount and GetChild:
int count = transform.childCount;
for(int i = 0; i < count; i++)
{
Transform child = transform.GetChild(i);
// ...
}