- Home /
how do i tell how many childs a object has
Im making a script that loops threw childs of a object and i need the script to know how many childs it has so that it know when to go back to 0.
here's a demonstration
var holder : GameObject;
var number : int = 1;
function Update()
{
if(somthing Happens)
{
number += 1;
//some how make this "if" know when "holder" runs out of childs
if()
{
number = 0;
}
}
}
thanks in advance :] and thumb up this question plz
Answer by hirenkacha · Oct 28, 2013 at 07:03 AM
Declare an array containing the children :
Transform[] children
.Populate the array as follow:
children = gameObject.GetComponentsInChildren<Transform>();
You can now check the length of the array using
children.Length
to get the total number of children
This will work, however note that this may be more computationally intensive than simply calling transform.childCount
. Since the question doesn't mention modifying the objects and just getting the count, you do not necessarily need references to the objects just to get the count.
Answer by guinoalaskapp · Jun 13, 2021 at 06:40 PM
Also Transform.childCount works too
Yeah, why is the over-complicated solution marked as correct?
That's much easier, but it is with transform.childCount, without the T capitalized.
Your answer
![](https://koobas.hobune.stream/wayback/20220613120756im_/https://answers.unity.com/themes/thub/images/avi.jpg)