- Home /
search in child of game object and insert theme in to array ?
Hi , how can i search in child of game object and insert theme in to array ? thanks
Answer by speedything · Oct 07, 2012 at 09:09 AM
You want to put all children of an object into an array? I'd imagine a list would work better, so I'll give you that code. It should be fairly simple to change this code to an array if you really want.
// This will work if placed on object with children.
public List<GameObject> myList = new List<GameObject>;
void AddChildrenToArray()
{
foreach (Transform child in transform)
{
myList.Add(child)
}
}
i made transform array to insert child of gameobject into it , i need to use transform array , how can i convert List to array ? is it possible ?
If you really have to use an array then just assign each child to the specific slot you want. For example,
int index = 0; foreach (Transform child in transform) { myArray[index] = child; index++; }
You'll need to make sure the array is the right size though, preferably when created. If you're going to be resizing it depending on the number of children then you really should be using a list.
Your answer