• 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 Essential · May 05, 2012 at 05:17 AM · getcomponentparent

Accessing parent gameobject script not working

Any ideas why the console keeps returning null? The parent gameobject has a script attached named "AIFollow".

 private var AIFollowScript : AIFollow;
 
 function Start () {
     AIFollowScript = transform.parent.gameObject.GetComponent(AIFollow);
     Debug.Log(AIFollowScript);
 }
Comment
Add comment · Show 6
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 chainedlupine · May 05, 2012 at 06:12 AM 0
Share

$$anonymous$$aybe this object's parent is null, too? :)

avatar image hijinxbassist · May 05, 2012 at 06:24 AM 0
Share

As per chainedlupine s comment

 function Start () {
     Debug.Log(transform.parent.gameObject);
     AIFollowScript = transform.parent.gameObject.GetComponent(AIFollow);
 }
avatar image Essential · May 05, 2012 at 06:52 AM 0
Share

Seems to grab the parent fine. Just the get component that isn't working…

avatar image Essential · May 05, 2012 at 06:53 AM 0
Share

In case I've made some obvious error I can't see, here's the component in the parent inspector: http://i.imgur.com/2HooP.png

avatar image hijinxbassist · May 05, 2012 at 07:00 AM 0
Share

@Essential Are you sure you are calling the correct gameobject? Try this debug

Debug.Log(transform.parent.gameObject.GetComponent(AIFollow));

maybe get the position as well, and pause the game and compare your 'parents' position to the printed position.

avatar image Essential · May 05, 2012 at 07:10 AM 0
Share

So I tried that and it definitely seems to be the correct gameObject. I even tried getting a different component and modifying it and that worked too.

I'm getting a script I haven't coded myself. It's also a C# script. Could there be some sort of protection on the script that won't allow me to get it? Any ideas what I would look for?

1 Reply

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

Answer by CHPedersen · May 05, 2012 at 12:21 PM

It's definitely not parent being null. If that were the case, the code would throw a NullReferenceException because you'd be trying to call GetComponent on something that was null, and that isn't the case.

This might be a shot in the dark too, but I think this is a script language thing. From the screenshot provided, it follows that AIFollow is a C# script, but the script you're calling it from is clearly JavaScript.

Please see this:

http://unity3d.com/support/documentation/ScriptReference/GameObject.GetComponent.html

And note, specifically, the sentence:

"It is better to use GetComponent with a Type instead of a string for performance reasons. Sometimes you might not be able to get to the type however, for example when trying to access a C# script from Javascript. In that case you can simply access the component by name instead of type."

This is exactly your situation. It's probably failing due to the type inspecificity of javascript versus the type safety of C#, and you're using the generic version of GetComponent in your code.

Try switching to the string version and fetch it using,

 transform.parent.gameObject.GetComponent("AIFollow"); 

instead.

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 Essential · May 05, 2012 at 06:10 PM 0
Share

Thanks for the info, didn't know that. Gave it a try but it still isn't working…

avatar image Essential · May 05, 2012 at 06:13 PM 0
Share

Ah, yes it did fix it actually. I just also had another script of the same name.

Thanks!

avatar image dronpes · Feb 27, 2015 at 05:02 AM 0
Share

This was obscure, and a great catch. Thanks CHPedersen.

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

7 People are following this question.

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

Related Questions

Talking To Other Scripts 1 Answer

How can i make GetComponent(SCRIPT) apply to ONLY ONE object that has the script? 1 Answer

How to activate parent camera of gameobject -1 Answers

Changing variables in another script 3 Answers

how to make one script change a variable in another scipt 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