Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 zachypin · Jun 28, 2011 at 05:08 PM · getcomponentcomponentgetcomponentinchildren

GetComponent in parent question

I know you can do GetComponent and it looks for other scripts in the same gameObject.
Then there's GetComponentInChildren which looks for scripts in the gameObject's children.

I was wondering if there was the same thing but for looking up in hierarchy like GetComponentInParent?
Or if GetComponent is already doing that.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
11
Best Answer

Answer by Eric5h5 · Jun 28, 2011 at 05:13 PM

transform.parent.GetComponent(...

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 zachypin · Jun 28, 2011 at 05:22 PM 1
Share

thx

avatar image
6

Answer by KingPeas_1 · Jun 19, 2014 at 03:37 AM

From 4.5 available GameObject.GetComponentsInParent

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 JamesL98 · Nov 03, 2014 at 11:16 AM

GameObject.GetComponent("Name In hierarchy").GetComponent("Name Of Script/Controller");

This will find the object and then select the chosen script. The Name In Hierarchy can be a child of another game object and it will still select it without specifying that it is a child.

Comment
Add comment · Show 3 · 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 solutionlab · Jun 04, 2019 at 07:50 AM 0
Share

is it also possible to get the parent itself ? So that I can activate/deactivate it?

$$anonymous$$ind regards, Stefan

avatar image Bunny83 solutionlab · Jun 04, 2019 at 09:09 AM 0
Share

Why do you post a comment to this misleading and wrong answer? GetComponent can not find gameobjects, only components. He probably wanted to write GameObject.Find(). Even then this is a bad approach since you're bound to unique object names and it would be quite slow. Furthermore if you plan to deactivate the gameobject GameObject.Find won't find it again.


To access the parent of a gameobject, just use transform.parent.gameObject. GameObjects do not have parents or child objects, only the Transform component builds up the hierarchy. When you look at Eric's answer at the top you'll see he suggested the same thing.


Next time when you have a question, please ask a seperate question. Though this is probably a too trivial question since just reading the documentation / manual should have answered your question. Also using google proberly should have given you this as the first result: How to disable parent through its own child object

avatar image solutionlab Bunny83 · Jun 04, 2019 at 12:25 PM 0
Share

Thanks for the answer! It´s not so easy for me to understand all the staff right and I am also new in Unity Program$$anonymous$$g. The documentation is often written for people who already have a lot of knowledge about program$$anonymous$$g in Unity. But thanks for the answer anyway :-)

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

9 People are following this question.

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

Related Questions

Unable to get component from a gameobject 0 Answers

How to Get TextMesh component from the Gameobject the script is attached to? 3 Answers

Using GetComponent in multiple scripts for same component? C# 2 Answers

How to access a Component from another Child when current is Destroyed 1 Answer

What is the difference between GameObject.GetComponent and Component.GetComponent? 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