- Home /
How to use a tag in children of an gameobject?
Hello all, I have a change-able character (a game object as a parent and there are 2 characters as child in it) (as an example you can assume a human can turn to a werewolf)
Now I can access to the parent`s tag easily and enemies work well. But what if I want a special enemy be able to attack to the tag "Human" only? (parent has a "Player" tag and child has "Human" and " Werewolf" tag) By changing the tag for enemy to follow then it is not work. (means in codes instead of using "Player" tag using "Human" or "Werewolf" which are child) So how can I use a tag in children? I am appreciate for any help.
void OnTriggerStay2D(Collider2D other)
{
if(other.tag=="Player" && ...)
Answer by ray2yar · Dec 27, 2018 at 12:00 AM
I am not sure based on your description what exactly you're going for. However, if you're shooting for collisions to only take effect based on the tag of the child gameobject's tag, that's certainly doable... but a better solution might be to use a script on the parent (player) that stores the state the player is in (werewolf vs human) and check against that instead...
You will have to get an array of the children gameobjects and check their tags. Assuming you disable the other child gameobject something like this should work:
void OnCollisionEnter(Collision collision)
{
if(collision.collider.tag == "Player")
{
int NumberChildren = collision.transform.childCount;
if (NumberChildren < 1) return;
for (int n=0;n<=NumberChildren; n++)
{
if(collision.transform.GetChild(n).tag == "something")
{
//do something
}
}
}
}
@ray2yar, Thanks. I am sorry if it explained in bad way as my English is not my native language.
I have a game object which is parent with tag of "Player". It has two game object children with tags "Human" and "werewolf". Now there are tree types of enemies. One can attack to both so I am gonna use tag "Player" with it and already tested and it is perfect. But for other types of enemies, one of them can attack to human only and other can attack to werewolf only. Now for these two enemies I should not use tag "Player" because I just want to attack to one of them at the time (and only of them is active at the time), just "Human" or "Werewolf". Now I am looking for a way so that TAG IN CHILDREN OF AN GA$$anonymous$$E OBJECT work. I thought I can make an enemy and use tag "Human" or "Werewolf" with that easily and it is gonna work but it is not working. and I am newbie, thanks for the script above, I tested but can`t work.
Answer by Refar8 · Dec 27, 2018 at 10:04 AM
OK, I found the problem myself. Just in case for whoever has same problem:
if you have an game object and you are gonna using the Tag of it then go head. Where ever you are using that tag it is gonna work perfectly as it is in parent and can know directly.
if you have a game object with one or several game objects in it as child and want to use the tag one of children not the parent, then you should use this to make it work: GameObject.FindWithTag("name" )
@ray2yar thank you again.
Your answer
![](https://koobas.hobune.stream/wayback/20220612184624im_/https://answers.unity.com/themes/thub/images/avi.jpg)