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
1
Question by DangerRanger · Jul 03, 2012 at 10:49 AM · gameobjectcolliderparentchildhierarchy

How can I access a Collider's GameObject in script?

I'm getting started on my first real Unity project and I began "training" my AI for the game. I want the AI soldiers (surprise, it's a shooter game) to see available places to take cover once the Collider of the cover collides with a Sphere Collider (a basic sight range, which is also a Trigger) I parented to the Soldier's GameObject.

In the Sphere Collider's script I have the OnTriggerEnter function that gives the Soldier's script the Collider that came into the Sphere Collider. That Collider is stored into a Collider variable in the Soldier script.

The problem is this: I don't know how to get GameObjects in the cover's hierarchy where the cover's Collider is. I'm trying to access the parent GameObject that is the whole cover with the mesh and it's collider objects/components, through which I could then access a child GameObject (just to mark a position) I placed next to the cover so the Soldier knows a position where he can hide.

How do you tackle problems like this? Is there an easy way to fix this or do you have a better way to do all of this in some other way? I'm running low on ideas right now...

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

Answer by aldonaletto · Jul 03, 2012 at 11:23 AM

The hierarchy is a Transform stuff, thus you need it to access any relatives. You have direct reference to the parent and root transforms, and can find by name any other object in the hierarchy with transform.Find("PathName"):

function OnTriggerEnter(other: Collider){
  // get the parent
  var daddy: GameObject = other.transform.parent.gameObject;
  // get another relative by name:
  var brother: GameObject = other.transform.parent.Find("Body/LeftLeg").gameObject;
  // go straight to the root:
  var bigBoss: GameObject = other.transform.root.gameObject;
}
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 DangerRanger · Jul 03, 2012 at 12:56 PM 0
Share

Thank you very much! I'll have to look into that!

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

6 People are following this question.

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

Related Questions

Collider working but not firing OnCollisionXYZ (in parent object) 0 Answers

collision.gameObject returning parent. 1 Answer

Make a simple tree 1 Answer

How can I access a child object? 1 Answer

Instantiate Terrain Object as child of Empty Game Object 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