How do you get child and grandchild?

I have a GameObject which contains children and grandchildren. I would like to change the layers of the entire Parent folder with another layer. Currently the parent and her children I was able to change layers, I add grand to child and it didn’t work, it was the same results as I typed child. I have 6 children who have children themselves.

newBuilding.gameObject.layer = 0;
foreach (Transform child in newBuilding.transform) child.gameObject.layer = 0;
foreach (Transform grandChild in newBuilding.transform) grandChild.gameObject.layer = 0;

Search: birds and the bees

You could do something like this. It calls SetLayer recursively on each child until it’s visited every one.

void Start(){
	SetLayer(transform.root, 1);
}

public static void SetLayer (Transform trans, int layer){
	//Set the layer Of the parent
	trans.gameObject.layer = layer;

	// Call set layer on any children
	for (int i = 0; i < trans.childCount; i++) {
		SetLayer(trans.GetChild(i), layer);
	}
}

Both of those lines do exactly the same thing. Calling the Transform “child” or “grandChild” is completely irrelevant.

You have to foreach through all the children of the child like so:

foreach (Transform child in newBuilding.transform)  
{  
  child.gameObject.layer = 0;  
  foreach (Transform grandChild in child)   
    grandChild.gameObject.layer = 0;  
}