- Home /
Getting a tag from a child
Hello! I have a script on Scroll-Snap and I want to read the tag "Bebop", I´ve read the api, I know it´s something about parents and children but I havent found a solution, hope anyone can help! : )
public class FondoSelector : MonoBehaviour { public int numFondo;
public void SelectFondo()
{
if (gameObject.CompareTag("Bebop"))
{
numFondo = 1;
}
}
}
p1.jpg
(70.4 kB)
Comment
Answer by SirPaddow · Feb 14, 2021 at 07:18 PM
There are many ways to find "Content" from Scroll-Snap. I usually prefer to keep a reference by creating a public attribute in my script, but if you would rather access through the hierarchy, this line should get you to "Content":
var contentTransform = transform.GetChild(0).GetChild(0);
No matter how you chose to get to "Content", you can then use transform.childCount and transform.GetChild(), something like this:
numFondo = -1; // -1 means "not found"
var childCount = contentTransform.childCount; // number of gameobjects that are directly children of "Content"
for (var i = 0; numFondo < 0 && i < childCount; ++i) {
if (contentTransform.GetChild(i).gameObject.CompareTag("Bebop")) {
numFondo = i;
}
}