Edited at

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

More than 3 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;
}
}

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