- Home /
Creating an array of immediate children of a game object
Hello,
Inside a game object, I am trying to make an array of other game objects which are its immediate children. It is easy to find how many immediate children the game object contains using transform.childcount, but the only methods that I can think of to store them as a collection (array) are very verbose.
To clarify the problem further, my GameObject is a MenuObject, which may contain submenu MenuObjects. I'm trying to find the submenu objects of any given MenuObject. I have tried numerous things, none of which were successful. At present, I have:
// Populate an array of submenu objects
submenuObjects = new MenuObject [transform.childCount];
for (int i = 0; i < transform.childCount; i ++)
{
Transform next = transform.GetChild (i); // this works
submenuObjects [i] = next.gameObject; // can't cast between MenuObject and gameObject
}
In this scenario, I have a reference to the GameObject of a MenuObject, but I don't know how to place that in my array. Is there a simple way to create an array of immediate children in C#?
Thank you in advance,
Peter
You could call this from the GameObject in question:
Transform[] children = GetComponentsInChildren< Transform >();
Then you'd have an array of the childrens transforms, and you can grab the child GameObject in question with
children[i].gameObject
Edit: Answer moved to comment. I didn't realize this was a recursive function, thanks @robertbu and @elpedro_75 for pointing that out.
Thank you for your response.
I tried using GetComponentsInChildren, but I found that it returned both the object in question and the grandchildren. Hence, if an object had two children, and one of those children had a child, it would return four values.
Assu$$anonymous$$g you are absolutely sure that all children have the '$$anonymous$$enuObject' script attached, the last line would be:
submenuObjects [i] = next.GetComponent<$$anonymous$$enuObject>();
Note your question does not indicate the real problem. That is, you want to build an array of a specific components of immediate children.
While not as clean, you could use GetComponentsInChildren() as @joshpawyer indicated and just ignore any entries where the transform.parent is not the transform above the immediate children.
Thank you, roberbu! That is exactly what I needed. I considered the second solution, but decided it was just getting too messy.
Yes, you are quite right; I should have stated my problem more clearly too.
Thank you, both, for your help.
Whoops, I broke UA. I converted my answer to a comment, this question now has -1 answers
Your answer
Follow this Question
Related Questions
Iterate through the list of child objects within a game objects 2 Answers
Find Child of GameObject from Array in Different Script 0 Answers
Use an objects (from array) position to focus a camera on 3 Answers
Cannot convert 'UnityEngine.Collider[]' to 'UnityEngine.gameObject[]' using OverlapSphere 3 Answers
Detection if a GameObject is below you or next to you? 1 Answer