• 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 code-blep · Sep 30, 2012 at 05:10 PM · transformparentname

How to find if a child belongs to a Transform on RayCast Hit [Updated]

Hi,

I have a raycast which is set to trigger when on a certain object. It does this by detecting the transform name.

However I now need to find out if the transform I have detected is a child of a specific parent transform.

[Updated]Is there a way to get the Root Parents Name of the Transform my RayCast Hit?

Here is my code so far:

 if(Physics.Raycast(transform.position, forward, hit, rayCastRange)){
          if(hit.transform.name == "AttackerTowPoint")
     {
         //Find out if hit.transform is a child of a GameObject called "Attacker"
     }
 }

Thanks

Paul

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
2
Best Answer

Answer by hvilela · Sep 30, 2012 at 09:21 PM

If you want to know if it's a directly child of "Attacker" you can do like:

 if (hit.transform.parent.gameobject.name == "Attacker"){
     // do whatever you want
 }

If you want to know if it's child of child (or at any deeper level) you can try something like this:

 Transform t = hit.transform;
 
 bool isChildOfAttacker = false;
 while (t.parent != null) {
     t = t.parent;
     if (t.gameobject.name == "Attacker") {
         isChildOfAttacker = true;
         break;
     }
 }

 if (isChildOfAttacker) {
     // do whatever you want
 }
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 code-blep · Oct 01, 2012 at 04:45 PM 0
Share

Perfect! Thanks Henrique!

avatar image BOT-Alex · Apr 05, 2020 at 10:32 PM 0
Share

Thank you for this.

avatar image
0

Answer by hvilela · Sep 30, 2012 at 05:20 PM

transform.parent returns the parent of a transform.

transform.parent.gameobject.name return the name.

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 code-blep · Sep 30, 2012 at 09:02 PM 0
Share

Hi hvilela,

Sorry but I have had to re-word the question, as it was worded badly, as a result your answer is not what I am looking for. Thanks for taking the time though.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

12 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

Related Questions

C#, expensive or inexpensive? 1 Answer

Properly Rotating Child Objects by Script 1 Answer

Problem retrieving transform.position after parent rotation 1 Answer

Make a GameObject child to a "Dragged Reference" GameObject doesn't work! 0 Answers

Following another object's position/rotation like parent/child relationship? 4 Answers


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