- Home /
Question by
Brahim113 · Aug 24, 2013 at 10:38 AM ·
getcomponentinchildren
Trying to get transform in child object
I have a small problem in my code. I am trying to get the child objects transform. But i only get the mainobjects transform.
bool UnitInTheWay(BuildingInfo building)
{
Transform trf = building.GetComponentInChildren<Transform>();
Collider []Colls = Physics.OverlapSphere(trf.localPosition,trf.localScale.x * 0.55f);
for(int i = 0; i < Colls.Length; i++)
{
Debug.Log(Colls[i].tag);
if(Colls[i].tag == "Unit")
{
return true;
}
}
return false;
}
BuildingInfo is a script for the mainobject. Any help is appreciated
Comment
Best Answer
Answer by KiraSensei · Aug 24, 2013 at 02:31 PM
You can use :
foreach (Transform child in transform)
{
...
}
This will loop on the children and "child" will be their transform component.
If you want to go deeper, you also have :
Transform[] allChildren = GetComponentsInChildren<Transform>();
foreach (Transform child in allChildren) {
...
}
This will go threw all children of the structure, and their children's children ...
Then in the loop you chose you can check the tag as you did.