FirstChildOrDefault

using System;
using UnityEngine;

public static class TransformEx
{
    public static Transform FirstChildOrDefault(this Transform parent, Func<Transform, bool> query)
    {
        if (parent.childCount == 0)
        {
            return null;
        }

        Transform result = null;
        for (int i = 0; i < parent.childCount; i++)
        {
            var child = parent.GetChild(i);
            if (query(child))
            {
                return child;
            }
            result = FirstChildOrDefault(child, query);
        }

        return result;
    }
}
Example:
var cube = this.transform.FirstChildOrDefault(x => x.name == "deeply_nested_cube");

Description

Finds first occurrence of a Unity Transform that satisfies the predicate

Details

Double click on the code to select all.

 

;