• 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 oliver-jones · Apr 07, 2011 at 03:13 PM · getcomponentparent

GetComponent To Read Up Hierarchy?

Hello,

I have a game object called MissileLauncher, within that I have a child called Missile. The Missile has a script on it that needs to change a variable in a script attached to the MissileLauncher.

I try to use GetComponent, but I always seem to get a null when getting a component up the hierarchy (on the parent). Is there a way to do this? I've been using all the examples that the Scripting API gives, and I can never do it?!

Okay, here is my layout:

MissileLauncher (Parent), contains variable:

var timeInterval : float = 2;

Missile (Child), needs to talk to 'timeInterval':

getMissileLauncher = gameObject.GetComponent("MissileLauncher");
getMissileLauncher.timeInterval = 5;

Every time I play, I get a null error. Also, I don't want to use statics.

How do I talk upward?? Driving me crazy!

Thanks folks!

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

Answer by Mike 3 · Apr 07, 2011 at 03:18 PM

Try this:

getMissileLauncher = transform.root.GetComponentInChildren.<MissileLauncher>();

It'll get the MissileLauncher script no matter which parent/sibling it's in.

Alternatively:

getMissileLauncher = transform.parent.GetComponent.<MissileLauncher>();

Will get the script from one parent up.

Or:

var missileLauncher : MissileLauncher;

Declared at the top of your file, it'll let you drag in the missile launcher at editor time, so you don't need to getcomponent all the time

Comment
Add comment · Show 6 · 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 oliver-jones · Apr 07, 2011 at 03:24 PM 0
Share

But I have multiple missile launchers (turrets) in the scene which each need a unique timeInterval ... this wont affect those scripts will it?

avatar image Mike 3 · Apr 07, 2011 at 03:30 PM 0
Share

No, it'll only affect the ones in the specific hierachy. I'll add some alternative methods for you in any case.

avatar image oliver-jones · Apr 07, 2011 at 03:34 PM 0
Share

This isnt working for me anyway: function Update(){ get$$anonymous$$issileLauncher = transform.root.GetComponentInChildren.<$$anonymous$$issileLauncher>(); get$$anonymous$$issileLauncher.timeInterval = 3; }

Its still not changing the value - although, I'm not getting a null error

avatar image oliver-jones · Apr 07, 2011 at 03:43 PM 0
Share

I have no idea what I'm doing wrong - but it just wont talk!

avatar image oliver-jones · Apr 07, 2011 at 03:54 PM 0
Share

Humm - I've even made it a static var and its still not being changed - must be a problem else where -- thanks anyway!

Show more comments

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

No one has followed this question yet.

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

Talking To Other Scripts 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