Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Refar8 · Dec 26, 2018 at 11:25 PM · tagtagging

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" && ...)
       
         
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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
                 }
             }
         }
     }

 
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Refar8 · Dec 27, 2018 at 09:32 AM 0
Share

@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.

avatar image
0

Answer by Refar8 · Dec 27, 2018 at 10:04 AM

OK, I found the problem myself. Just in case for whoever has same problem:

  1. 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.

  2. 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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

97 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Game wont restart in the build, but works great in the editor.. 1 Answer

Destroying Multiple Objects with Tags 1 Answer

Set a Tag Multiple GameObject[] 1 Answer

How do you find the tag of the object that is a trigger? 1 Answer

Match 3 game getting the colour of the match 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges