Skip to content

Instantly share code, notes, and snippets.

@enpel
Last active August 29, 2015 14:21
Show Gist options
  • Save enpel/b83b2da041c7def548c7 to your computer and use it in GitHub Desktop.
Save enpel/b83b2da041c7def548c7 to your computer and use it in GitHub Desktop.
親の親の親の...親からGetComponentしたい!!そんな拡張 ref: http://qiita.com/enpel/items/7d9eb414a92f15ea694a
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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment