How do I alter all children of a gameobject at once?

I have a very small city for my game. The idea is that the player shoots the buildings in the city and destroys them. But, as I said, the game board is very small, and to destroy all the buildings takes mere seconds. I’d like to make it possible to destroy buildings, and make them all re-appear at the push of a button. As of right now, the buildings are not truly destroyed, but rather are deactivated, using

gameObject.SetActive(false);

In a separate script, I have

if(Input.GetKeyDown(Keycode.Return)) 
{
     buildings.SetActive(true);
}

where “buildings” is a public gameobject. In the editor, the gameobject “buildings” is assigned as an empty gameobject containing the other buildings in my scene as children.

This solution as I have it doesn’t work. I figure it is because I am disabling the buildings individually, but enabling the parent object, leaving the children untouched. My question is this. What code do I need to use (C# please!) to SetActive(true) for all of the children of the “buildings” gameobject?

I suppose I could simply assign all of the individual buildings as their own gameobject in the script, and activate them as such

building1.Setactive(true);
building2.Setactive(true);
building3.Setactive(true);
.....

but that is kind of exhausting, especially when I expand my map to include more buildings. There must be a way to do this all at once, or at least a way more efficient than doing them all individually. Any help appreciated.

You can do this one of two ways. The easiest way is to create a public Array and assign all of your buildings to it manually. This is generally what I would do unless the building children are getting generated dynamically. If they are generated dynamically, then you would assign the GameObjects array via code.

However, based on your scenario, it sounds like this will work:

public GameObject[] buildings;

Then iterate through each of your buildings with a for loop.

for(int i = 0; i < buildings.Length; i++) {
  buildings*.SetActive(true);*

}
alternatively, you can actually find/manipulate the children by doing this:

  •   for(int i = 0; i < building.transform.childCount; i++) {*
    
  •   	building.transform.GetChild(i).gameObject.SetActive(true);*
    
  •   }*
    

**Edited to fix code error.