- Home /
Get Children of Child and Deactivate/Activate on Demand
So here is my layout, I have a 3D Menu system working and they are all in a parent/ child relationship. So for example I have my GuiRoot variable which is just an empty game object then the child of that GuiRoot is going to be my MainMenu. I then have children inside of MainMenu each child has a bunch of siblings as well so it looks kind of like this:
So when i need to transition to my next menu my camera is promted to tween to the CameraLookAt and CameraPosition. I then get the name of the Parent of the currentMoveToPosition and compare my childs name with that name which should be MainMenu( and it is) but i then need to get all of the children of MainMenu and activate them which is what i am trying with the second for each.
My Problem is that the second for each isn't firing and I do not understand why not. The Print("in grouping"); never gets sent and so neither does the if below. The Printing of the currentMOveToTarget.parent.gameObject.name and guiChild.name DOES however fire. and everything inside of gui gets turned off..... I dont understand the issue with my code.
This is all a bit complicated and I don't know if i explained it very well. So if you need more explanation I can expand.
allGuiChildren = guiRoot.GetComponentsInChildren<Transform>();
// Turn off all GUI cases except for the Main Menu
foreach( Transform guiChild in allGuiChildren )
{
print(" in gui children");
// get the parent of the current move to target in this case it is main menu
if( guiChild.name == currentMoveToTarget.parent.gameObject.name)
{
Transform[] currentGuiGrouping;
currentGuiGrouping = guiChild.GetComponentsInChildren<Transform>();
print(" in current Move to target" + currentGuiGrouping);
foreach( Transform guiGroupingChild in currentGuiGrouping)
{
print("in grouping");
if(guiGroupingChild.name == "GuiBase"){
print(guiGroupingChild.name);
}
}
print(currentMoveToTarget.parent.gameObject.name + " " + guiChild.name);
// figure out how to set the children of mainmenu true
}
else
{
guiChild.gameObject.SetActive(false);
}
}
Thanks in advance!
Answer by TonyLi · Aug 16, 2013 at 02:03 PM
Try:
GetComponentsInChildren<Transform>(true)
to find the transforms of inactive objects, too.
Also, you can iterate over a transform itself. So you can do this instead:
// Turn off all GUI cases except for the Main Menu
foreach( Transform guiChild in guiRoot.transform ) {...}
and
foreach( Transform guiGroupingChild in guiChild.transform ) {...}
Makes the code simpler.
so what is the point of GetComponentsInChildren if you can just go through it with a foreach?
The foreach iterates through the transforms, but only the transforms. With GetComponentsInChildren you can get an array of any type of component (collider, renderer, etc).
Sorry if this sounds dumb, but wouldnt that solution only acess the children of child components? What if I need to enable the gameobject (that is a children of a child) itself?
Every component has a reference to its GameObject. So you can reference guiGroupingChild.gameObject. Also, GetComponentsInChildren() is recursive, so it'll get grandchildren and so on, while foreach over the transform will only give direct children.
Your answer
Follow this Question
Related Questions
Make a simple tree 1 Answer
Switch for children not working 1 Answer
How to move the parent object to a child of one of its children? 2 Answers
Multiple Cars not working 1 Answer
How To Get List of Child Game Objects 14 Answers