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;
}
Written by Bruno Mikoski
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Unity
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#