- Home /
Making an array of specific children inside Hierarchy
So I have a set of objects inside a group like this :
The Ref_Floor
is a prefab that exists several times and I would like to make an array that contains all the Ref_Laying
and another one that contains all the Ref_Standing
(they are always inside the parent REFERENCES)
So right now I'm doing this to get an array with all the Ref_Floor:
var actorFloorTriggerParent : Transform; //This is the REFERENCES transform
var actorFloorTriggerArray : Transform [];
//On Awake ()
actorFloorTriggerArray = new Transform [actorFloorTriggerParent.childCount];
for (var i=0; i < actorFloorTriggerParent.childCount; i++){
actorFloorTriggerArray[i] = actorFloorTriggerParent.GetChild(i);
}
But now I'm not sure how I can get the arrays for the Ref_Laying and Ref_Standing. This is what I was trying so far. My idea was to look for the children by name and add them but the code isn't really working:
actorFloorStandArray = new Transform [actorFloorTriggerParent.childCount];
actorFloorLayArray = new Transform [actorFloorTriggerParent.childCount];
for (var child : Transform in actorFloorTriggerParent){
if (child.name == "Ref_Laying"){
for (var c=0; c < actorFloorTriggerParent.childCount; c++){
actorFloorLayArray[c] = child;
}
}
}
Any help very appreciated!
Your code only searches the immediate children. There are a number of posts on how to search all children. Here is one:
http://answers.unity3d.com/questions/10417/how-can-i-access-the-children-of-a-transform.html
Thanks. I still couldn't figure out how to iterate through those children and then add them to the built-in array. Ended up using a generic list.
Your answer
![](https://koobas.hobune.stream/wayback/20220613111641im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Hierarchy index of prefab change when putting in into an array? 1 Answer
Iterating through multidimensional arrays 2 Answers
Comparing a string to an array extraction? 1 Answer
Get JSON array object string value 2 Answers
Getting the rotation of an object from another object within a prefab. 3 Answers