LoginSignup
0
0

More than 5 years have passed since last update.

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

Last updated at Posted at 2015-05-19

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;
    }
}

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

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0