- Home /
Find GrandChildren of Grandparent Gameobject by tag.
Trying to find the grandchildren of a GameObject by using tags to put in a list of Gameobjects that is attatched to the grandparent gameobject.
However, this code is finding ALL the gameobjects with the tag instead of only the gameobject with the tag that are the grandchildren of the Gameobject.
foreach (Transform child in transform)
{
if (child.tag == "ScaleThisCube")
{
if (!child.Equals(transform))
{
cubeobjects.Add(child.gameObject); //first list of gameobjects (puts children with tag ScaleThisCube into a list)
}
}
}
foreach (Transform child in transform) // ThisForeach loop is not working however....
{
cubeobjects.Add(transform.gameObject);
foreach (Transform grandChild in child)
{
if (grandChild.tag == "CubeFace")
{
if (!grandChild.Equals(child))
{
cubefaces.Add(grandChild.gameObject); //second list of gameobjects (puts children of gameobject in the list "CubeObjects" with tag CubeFace into a list)
}
}
}
}
Comment
ok, so why is CubeObjects not a list of Transforms? why does the second loop not iterate CubeObjects but again the Transforms?
CubeObjects is a list of gameobjects because I need to iterate over that list for a specific function. And, I thought I was iterating over the cubeobjects list?
Answer by bennett_apps · Oct 08, 2018 at 11:13 PM
try using this logic:
transform.GetChild(indexOfChild).gameObject.FindWithTag("WhateverTagYouWant");
You may have to tweak this to your personal needs :D