- Home /
Question by
Xeeraj10 · Oct 09, 2017 at 01:27 PM ·
arraygameobjects
how to get child objects transform in arry ?
Actually its a kind of weird problem i am trying to get child objects transform in arry
public GameObject way;
public GameObject[] myway;
void Start ()
{
for (int i = 0; i < way.transform.GetChildCount(); i++)
{
Debug.Log (way.transform.GetChild (i).name);
myway[i] = way.transform.GetChild (i).transform;
}
}
debug.log work well without getting objects in arry code line "myway[i] = way.transform.GetChild (i).transform" but when i place this line of code it just show first game object.
Comment
Answer by goutham12 · Oct 09, 2017 at 01:47 PM
you have to use array when you know the length. here you don't know length so take a list
like
public List<GameObject> childList = new List<GameObject>();
void Start(){
foreach (Transform t in transform) {
childList.Add (t.gameObject);
}
}
Your answer
Follow this Question
Related Questions
How to deactivate gameObjects instead of destroying them ? 1 Answer
Game Object Chains 1 Answer
Filling array with gameObjects 2 Answers
GameObject Array in Editor GUI 3 Answers