Help us understand the problem. What is going on with this article?

親の親の親の...親からGetComponentしたい!!そんな拡張

More than 5 years have passed since last update.
using UnityEngine;
using System.Collections;

public static class GameObjectExtensions
{
    public static T GetComponentInParents<T>(this Transform obj) where T : MonoBehaviour, new()
    {

        Transform parent = obj.transform.parent;
        while(parent != null)
        {
            T res = parent.GetComponent<T> ();
            if (res != null)
                return res;
            parent = parent.parent;
        }

        return null;
    }
}

無限に親を探し続けるので注意…

enpel
unity-game-dev-guild
趣味・仕事問わずUnityでゲームを作っている開発者のみで構成されるオンラインコミュニティです。Unityでゲームを開発・運用するにあたって必要なあらゆる知見を共有することを目的とします。
https://unity-game-dev-guild.github.io/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away