Handy piece of code that will run an action on all children of a gameobject.

using System;
using UnityEngine;

public static class UnityExtensions
{
    /// <summary>
    /// Run an action on all children of a gameobject
    /// </summary>
    public static void RunOnChildrenRecursive(this GameObject a_go, Action<GameObject> a_action)
    {
         foreach (var child in a_go.GetComponentsInChildren<Transform>(true))
             a_action(child.gameObject);
    }
}

To use it, do something like this: –

 myGameObject.RunOnChildrenRecursive(child => child.layer = LayerMask.NameToLayer("UI3D"));