Hey guys,
I would like to know if there is a way to detect if an object is a child of another through an if statement:
function Update(){
if(transform.isChild){
//do something
}
else{return;}
}
I know ‘isChildOf’ but I would like to know if there is a simpler solution.
1 Like
yes, you can check if an object is a child of another through an if statement. No, it is probably not any simpler than Transform.IsChildOf ,since that is designed to do exactly what you want…
You want to use IsChildOf IN an if statement… notice the line(in the link):
if (col.transform.IsChildOf(transform))
that is the only if statement you should need…
your example doesn’t actually make sense though, because there is nothing you are comparing your transform to…
if you have an example of the code you are trying to fix, I’d be happy to elaborate…
Simply check if transform of object is not the root.
if (transform.root != transform)
Debug.Log(gameObject.name + " is a child");