• 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 Zodd · Aug 14, 2013 at 01:03 AM · parentaccess

How to access the variable of a parent's script?

I realize this question has been asked, but I've tried all of the solutions I can find to no avail. I'm wondering if there is something about my circumstances that is causing the issue. I have a fire particle effect that gets instantiated on an object as it's child, and then attempts to lower the health variable of it's parent. I've tried the following code:

 gameObject.transform.parent.GetComponent(EnemyData).health -= 3;

and

 transform.parent.GetComponent(EnemyData).health -= 3;

and

 gameObject.transform.parent.gameObject.GetComponent(EnemyData).health -= 3;

etc

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 Zodd · Aug 14, 2013 at 12:39 AM 0
Share

I forgot to mention I am getting a null reference exception and I've made sure my variable names are accurate many many times :/

avatar image Joyrider · Aug 14, 2013 at 01:07 AM 2
Share

is your health variable in the EnemyData script declared as public? Are you coding in C# or Javascript?

avatar image bodec · Aug 14, 2013 at 01:14 AM 0
Share

you should cast to a variable so you have enemyData = gameobject.GetComponent (EnemyData) then its as simple as enemyData.doSomething.

avatar image Joyrider · Aug 14, 2013 at 01:16 AM 0
Share

@bodec using a variable doesn't cast anything and doesn't change anything to what he already tried... it is just a way to easily re-use that same component :)

avatar image bodec · Aug 14, 2013 at 01:20 AM 0
Share

correct sorry I failed to mention what I put in wasn't a fix but a way to improve performance. $$anonymous$$y appoigize

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by aldonaletto · Aug 14, 2013 at 01:23 AM

You could simply use the second alternative:

 transform.parent.GetComponent(EnemyData).health -= 3;

But since you're getting null reference errors, either the object has no parent or its parent has no EnemyData script - if health was misspelled, the error would be different (something like health is not part of EnemyData or similar). Double check the hierarchy, or add debug lines to know what's happening:

 var daddy: Transform = transform.parent;
 if (!daddy) print("Object has no parent");
 var script: EnemyData = daddy.GetComponent(EnemyData);
 if (!script) print("Parent has no EnemyData script");
 script.health -= 3;
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 Zodd · Aug 14, 2013 at 02:16 AM 0
Share

I think I might know what the problem is. The object is instantiated with a script on it that subtracts the health and whatnot, and then on the parent object's script it sets the instantiated object as it's child on the line right after it's instantiated. So the awake functions on the child are being triggered before it is set as the child to the parent object. Is there any way to instantiate it AS the child?

avatar image Zodd · Aug 14, 2013 at 02:22 AM 0
Share

I suppose worst case scenario I can just control it all with booleans, but instantiating it as a child would be less code.

avatar image aldonaletto · Aug 17, 2013 at 01:11 PM 0
Share

You could move the code related to the parent from Awake to Start: Awake is called when the object is created (before Instantiate returns, I believe), while Start executes some time after, after the calling routine that created the object has finished.

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

18 People are following this question.

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

Related Questions

Calling parent variables from child and visa versa 1 Answer

Access a child from the parent or other gameObject. 2 Answers

Accessing a variable from a child object and pass it to the parent 2 Answers

Make a simple tree 1 Answer

Access parents other child from other other child 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