Last Updated: February 25, 2016
·
490
· badawe

Find component upwards

Here is a extension to find one component upwards, is usefull when you have a lot of prefabs and need some dynamic linkage hierarchy

public static T FindComponentUpwards<T>(this Transform pTarget) where T : Component
    {
        Transform tTransform = pTarget.parent;
        while (true)
        {
            T tComponent = tTransform.GetComponent<T>();
            if (tComponent == null)
            {
                if (tTransform.parent)
                {
                    Transform tParentTransform = tTransform.parent;
                    tTransform = tParentTransform;
                    continue;
                }
                break;
            }
            return tComponent;
        }
        return null;
    }