Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by LexGear · Nov 30, 2012 at 08:13 AM · variablevaluestransferanother

Transfer values from one script to another

Hi Guys, this question has been asked many times but I can't seem to find one exactly like my scenario.

I have 3 Scripts, in the Hierarchy, they are each under one another like so:

ScriptA
ScriptB
ScriptC

ScriptA has the value I wish to give to ScriptC.

It's important to note that "STATIC" is not an option.

So far, I've worked out that I can get a value from B to C, by using on scriptC: varX = transform.parent.GetComponent(ScriptB).varY;

But using this same method from A to B results in the following error: NullReferenceException EnemyBodyScript.Update () (at Assets/Scripts/ScriptB.js:27)

I've also tried with root instead of parent, which resulted: NullReferenceException: Object reference not set to an instance of an object ScriptB.Update () (at Assets/Scripts/ScriptB.js:27)

If anyone could help I would be very grateful.

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 LexGear · Nov 30, 2012 at 08:14 AM 0
Share

I should note, B is the child of A and the parent of C.

avatar image fafase · Nov 30, 2012 at 08:25 AM 0
Share

Stupid question but are you using the appropriate script name as parameter? Just in case.

avatar image LexGear · Nov 30, 2012 at 08:26 AM 0
Share

Hi fafase, I'm just inspecting that link you gave a moment ago. and yep I'm using the appropriate script name in the parameter.

avatar image AeonIxion · Nov 30, 2012 at 08:32 AM 0
Share

I'm guessing the object your trying to access is empty. Like..a GameObject variable will be null if you did not assign an object to it.

avatar image LexGear · Nov 30, 2012 at 08:34 AM 0
Share

If I understand what your saying Aeonixion, then, no, the value is infact there. i.e. var life = 50;

Show more comments

1 Reply

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

Answer by Tim-Michels · Nov 30, 2012 at 08:33 AM

Your hierarchy is very important here, let's say you want to acces a value from script B inside your script A...

You can do that like this: (inside your ScriptA)

GetComponentInChildren(ScriptB); //Will work inside ScriptA

GetComponentInChildren(ScriptC); //Will also work inside ScriptA

When you want to acces a variable from script A inside your script B:

transform.parent.GetComponent(ScriptA); //Will work inside ScriptB

GetComponentInChildren(ScriptC); //Will also work inside Script

From inside your script C you can acces the other scripts like this:

transform.root.GetComponent(ScriptA); //Will work inside script C

transform.root.GetComponentInChildren(ScriptB); //Will work inside script C

or

transform.parent.GetComponent(ScriptB); //Will also work inside script C

I hope you understand what I'm doing here, basically the GetComponentInChildren is a very usefull function that can be used to get a ScriptC component, even if it's called on a ScriptA gameObject.

I hope I'm making any sense to you ;) Cheers

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 LexGear · Nov 30, 2012 at 08:42 AM 1
Share

Thank you Tim, that was very helpful. I would have figured it out on my own HOURS ago, but as it turns out, somewhere in my script, something was making ObjectB detach itself from ObjectA. Once I resolved that, using Root to jump from ScriptC to Script A worked awesomely. Thank you very much and to everyone else for your help. I love the unity community.

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

14 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

Related Questions

Cannot access a variable in another Script. 2 Answers

How Do I Modify This Variable in Another Script? 1 Answer

Accessing variable from another script 1 Answer

Can't access variable from another script. 1 Answer

enable disable variable of a script from another script 4 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