• 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 Tabu · Aug 30, 2010 at 07:52 PM · getcomponentparent

"GetComponentInParent" - How do I do it?

Have been looking for an easy way get a Component looking the other way than in the children, but there seems to be no easy way? Have I missed somthing in the documentation, or is there only the hard way to do it? (ps. I made up GetComponentInParent, since it was the easiest way to explain my problem)

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

Answer by burnumd · Aug 30, 2010 at 09:48 PM

Try

transform.parent.GetComponent<YourComponent> ()

(where, obviously, "YourComponent" is your component). If you want to search from the top down of your object, you can also get

transform.root.GetComponentInChildren<YourComponent> ()

Both "parent" and "root" are members of the Transform class which you should have access to if your class extends MonoBehaviour. The latter would be a pretty intense operation for frame-by-frame updates, but would suffice for caching in Awake()/Start().

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 Tabu · Aug 31, 2010 at 07:53 AM 0
Share

I think I love you!

avatar image
1

Answer by Tabu · Aug 30, 2010 at 08:32 PM

Ok, I feel a bit stupid, but it still might not be the best way to do it. But this is how I did it: I have two scripts, WeaponScript and MovementScript, the WeaponScript is in a childHieracy below the MovementScript. So I start out in the MomentScript finding the WeaponScript, then in the WeaponScript I make a function where I can push the MovementScript.

MovementScript takeout:

    void Start()
{
    myWeapon = (WeaponScript)this.GetComponentInChildren<WeaponScript>(); //Here i get the WeaponScript
    myWeapon.setMovementScript(this); //Push this script into the Weaponscript
}

WeaponScript TakeOut:

private MovementScript moveScript; //Makes a MovementScript variable.

public void setMovementScript(MovementScript x) // This is the function called in movementscript. { moveScript = x; }

I will still absolutely like to hear about alternatives to this way of dooing it, as I am not completly sure this is the best way to do it!

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
3

Answer by Lasse-Loepfe · Jun 05, 2014 at 01:29 PM

Since Unity 4.5, you can directly call GetComponentInParent()

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 pengo · Jun 13, 2014 at 09:41 PM 0
Share

Docs are here:

http://docs.unity3d.com/ScriptReference/Component.GetComponentInParent.html

Note: helpfully GetComponentInParent will look at all parents (e.g. in the parent of parent). And there is also a GetComponent*s*InParent function.

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

1 Person is following this question.

avatar image

Related Questions

How to activate parent camera of gameobject -1 Answers

GetComponent searches an objects child instead of the object 2 Answers

Connecting Multiple Rigidbodies with Collision 0 Answers

Access component of parent with multiple children 2 Answers

GetComponent() vs Delegate/Event 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