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 antoinne85 · Nov 20, 2011 at 03:29 PM · capsulecolliderrequirecomponent

How Can I Get the Parent GameObject from Within a Component (C#)

I have a GameObject that will share a common feature with a bunch of other GameObjects. When an NPC runs into it, I want to perform an action.

I've started scripting a class called Trap. My intention was to require a CapsuleCollider, and then use variables inside the Trap class to control some of the relevant parameters of the CapsuleCollider.

The problem that I'm having is that if I want to do this then I need access to the CapsuleCollider that I'm requiring. I'm very new to Unity and I'm having difficulty figuring out what I need to do to get to the GameObject that owns this particular instance of the component (or it's sibling components).

Any help is apprecaited.

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

4 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by stng · Jun 29, 2018 at 04:43 AM

You can use Component.gameObject

Link: https://docs.unity3d.com/ScriptReference/Component-gameObject.html

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
avatar image
1

Answer by aldonaletto · Nov 20, 2011 at 03:43 PM

Child and component aren't the same thing: components are added directly to the GameObject, while children are complete GameObjects whose transform.parent property is set to the parent Transform. To get a component of a GameObject, you use GetComponent:

   var capsule: CapsuleCollider = GetComponent<CapsuleCollider>();

Or, in C#:

   CapsuleCollider capsule = GetComponent<CapsuleCollider>();

This instruction gets the CapsuleCollider component of the object to which the script is attached. If you need to get the CapsuleCollider of another object, you must have a reference to it - its transform, gameObject, collider etc.

   var otherObj: Transform; // drag the other object here
   ...
   var capsule: CapsuleCollider = otherObj.GetComponent<CapsuleCollider>();
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
avatar image
0

Answer by DaveA · Nov 20, 2011 at 03:32 PM

You can use Tags and use http://unity3d.com/support/documentation/ScriptReference/GameObject.FindWithTag.html or keep track of all the instances in an array or list or something. And remember that when collision is detected, it gives you the object that collided, and you can get at its components from that.

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 antoinne85 · Nov 20, 2011 at 03:43 PM 0
Share

So it sounds like I should add the "stuff that happens" on collision to the Trap, but that I should modify the collision boundaries directly on the CapsuleCollider ins$$anonymous$$d of trying to alter them through the Trap component?

avatar image
0

Answer by lezo · Jul 13, 2014 at 07:29 PM

(current_component as MonoBehaviour).gameObject

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

6 People are following this question.

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

Related Questions

How do you prevent a base class from being addable as a script without making it abstract? 0 Answers

Capsule doesnt collide capsule properly 0 Answers

RayCast can't detect GameObject at Player's feet 0 Answers

Adding a rigidbody and capsule collider to my player makes it teleport along the Y axis while moving 0 Answers

RequireComponent script order 2 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