How to find out if an element is the first child of some other game object in unity?Unity GetComponentsInChildren method return all children which is not what's needed. So I'm using this method for this purpose instead.
/**
* find out if child is first level child of given GO
* @return Transform
**/
bool IsFirstLevelChildOf(Transform root, Transform child)
{
bool isFirstLevel = false;
var children = root.GetComponentsInChildren<Transform>();
foreach(var c in children) {
if (c.parent == root && c == child) {
isFirstLevel = true;
break;
}
}
return isFirstLevel;
}
//------- Usage example ----------
var yourParentElement = this.gameObject.GetComponent<SomeComponent>().content;
if ( IsFirstLevelChildOf(yourParentElement.transform, this.transform))
{
//do something amazing
}