public static T[] FindComponents <T>(this UnityEngine.GameObject g, bool in_parent = true, bool in_children = true, int sibling_depth = 0, bool ignore_self = false) where T : Component { HashSet <T> components = new HashSet <T>(); if (ignore_self) { if (in_children) { foreach (Transform child in g.transform) { components.AddOrConcat(child.GetComponentsInChildren <T>()); } } if (in_parent) { components.AddOrConcat(g.transform.parent.GetComponentsInParent <T>()); } return(components.ToArray()); } if (!in_children && !in_parent) { return(g.GetComponents <T>()); } if (in_children) { components.AddOrConcat(g.GetComponentsInChildren <T>()); } if (in_parent && g.transform.parent) { components.AddOrConcat(g.transform.parent.GetComponentsInParent <T>()); } GameObject current = g; GameObject last = g; while (sibling_depth > 0) { current = current.transform.parent.gameObject; if (!current) { break; } components.AddOrConcat(current.GetComponentsInChildren <T>()); sibling_depth--; } return(components.ToArray()); }