Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Expedient Turnip · Dec 31, 2014 at 11:46 AM · c#collisiontriggerparent-child

Get child tag from parent-child collision

I have a setup where I am building a maze recursively with square tiles. To try and limit the squares that a maze tile can be placed in, I've set up each tile with several children box colliders. These children are supposed to toggle a boolean in the parent script, but the children's tags don't affect the parent script, so the booleans are never toggled. Can I use the tag from the child in the parent's collision script?

The C# snippet is from the parent object. open[] is a bool array, countOpen is a counter

    void onTriggerEnter (Collision other) {
             if (this.tag.Equals("box4")) {
                 open[3] = false;
                 countOpen--;
             }
             if (this.tag.Equals("box3")) {
                 open[2] = false;
                 countOpen--;
             }
             if (this.tag.Equals("box2")) {
                 open[1] = false;
                 countOpen--;
             }
             if (this.tag.Equals("box1")) {
                 open[0] = false;
                 countOpen--;
             }
     
         }

Is there a method objects can invoke to get the tag from a child object during a trigger event? If you need more details I'd be happy to supply them when I return.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by persijn · Dec 31, 2014 at 04:54 PM

 void onTriggerEnter (Collision other) {
      //this wil only find a single one
      if (this.transform.find("childname/orgrandchild").gameObject.tag =="box4") {
          open[3] = false;
          countOpen--;
      }
      //this is maby better for you:
      var gos = GameObject[];
      gos = GameObject.FindGameObjectsWithTag("box");
      foreach( GameObject taged in gos) {
          if (taged.tranform.gameObject.parent == this) {
              //You can name each box diffrent so your have a unique check
              if (taged.tranform.gameobject.name == "nameofbox") {
                  //do something
              }
          }
      }
 }

Extra:

From the child element

if you just want to set variables its easier from the child:

 void onTriggerEnter (Collision other) {
     if (other != this.transform.gameObject.parent) return;
     if (this.CompareTag("box4")) {
         other.gameObject.open[1] == false
         other.gameObject.countOpen--;
     }
 }
 
Comment
Add comment · Show 2 · 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 Expedient Turnip · Dec 31, 2014 at 10:24 PM 0
Share

Right, but this doesn't work when the tag is on the child, not the parent. $$anonymous$$aybe I could use a trigger or collision event on the child object and send a message to the parent?

avatar image persijn · Jan 02, 2015 at 09:10 AM 0
Share

I updated my answer. I think the extra should be the easiest way.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to call OnTriggerEnter once 5 Answers

Multiple Cars not working 1 Answer

I'd like some help with my health / shooting (collision detection) 1 Answer

Coin pickup script not working..? 1 Answer

Change scene with trigger collision not working. 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