How do I find the children of an object, and store them in an array?
I have a wall, with 4 points. I wan’t to be able to access these points, but in order to do that I need to have them in an array.
I use C#
How do I find the children of an object, and store them in an array?
I have a wall, with 4 points. I wan’t to be able to access these points, but in order to do that I need to have them in an array.
I use C#
For a one line method to get the array
Transform[] children = GetComponentsInChildren<Transform>();
Transform is also enumerable. So you can avoid the array altogether with:
foreach (Transform child in transform){
// Do something to child
}
Based on your questions I’ve seen here, you’d really benefit from spending an afternoon in the Learn Unity area of the website, where lots of tutorials address a ton of beginner’s questions while teaching good practices.
You probably want this:
Transform[] children = new Transform[transform.childCount];
for (int i=0; i<transform.childCount; i++) {
children*=transform.GetChild(i);*
}
List children = new List();
void Start()
{
foreach (Transform t in transform)
{
children.Add(t.gameObject);
}
}