How to disable all the children of a child in script?

I’ve looked around online and it seems like all the answers I see are for a situation in which the parent is running the script and the objective is to disable that parent’s children.

In this case, I have an object that is running a script. That object has children and those objects have children and so on and so forth. Way down the line of children, I have a set of children that I want to disable all at once.

How can I selectively disable THOSE children despite my script running on a parent of a parent of a parent? Here’s a picture of what I mean:

8151653--1058963--upload_2022-5-24_0-2-39.png

In this case, Canvas is running my script. I want to disable all the children of AugmentText (which is a child of Augments which is a child of Left Side which is a child of PauseMenu which is a child of Canvas).

Surely there must be some way for me to pass the AugmentText gameObject and disable all its children with a script running from Canvas?

Thank you for your time. I’m thinking the solution is right there but I’d appreciate some help.

Just disable the whole AugumentText game object. Disabling a game object also disables all it’s direct and indirect children. If you only want to disable children of AugumentText but not AugumentText itself add a dummy object between them so that you can easily disable whole subtree.

augumentText.gameObject.SetActive(false);

The levels of children between script which will do the work and object you want to disable doesn’t really matter. You can always create a field for reference to object you will need to access and assign it in editor.

That unfortunately doesn’t work with my purposes. Essentially, I need only one of these children objects to be enabled at a time. This means each time I enable one, I have to disable all of the rest. Some of my other objects have 10+ children. This gets incredibly difficult to keep track of in script when I have to manually disable each object one by one.

If I just disable the parent object and bring it back, all the previously enabled children will still be enabled which I can’t have. Do you know of a way that I could disable all the children directly?

Ok that requirement wasn’t clear from your initial post. If you wan’t something which shows one children at time, you can loop through the children and disable all except required one. Something like this

foreach (Transform t in augumentText.transform) {
    // t.gameObject.SetActive(false); // if you want to disable all
    t.gameObject.SetActive(t.gameObject == objectToShow); // hide all, except required one 
   // t.gameObject.SetActive(t == objectToShow.transform); // this also works
}

Just be careful when comparing objects. Make sure you are comparing GameObject with GameObject or Transform with Transform. Comparing different kind of components or component with GameObject will return false even when they belong to same GameObject.

2 Likes

Works like a charm, thank you so much for the help!

For the future, please use the scripting forum for scripting questions. Please only use the 2D forum for specific 2D questions, thanks.

1 Like