Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Edyvargas · Oct 13, 2016 at 08:11 PM · scripting problemgetcomponentvariablesvalue

How to READ a variable VALUE from other Object Script?

Hi, im trying to read a boolean variable value from other object script, but not sure how to do it right, im using this:

On the FROM object Script (called fromScript):

 public var myVariable : boolean; // The value that i want to send to other object script.
 
 function Start () {
 
 myVariable = true;
 
 }

On the TO Script on other Object:

 var fromObject : GameObject; // The object that contains the from script and the variable value.
 var theVariable : boolean;
 
 function Start () {
 
 theVariable = fromObject.GetComponent.<fromScript>(myVariable);
 // This value must be "true", cause it reads that value from the myVariable on the fromScript.
 
 }

and the theVariable value must be the same as the myVariable value on the from Object script, but this doesnt works.

Thanks to any help about this.

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

Answer by ElijahShadbolt · Oct 13, 2016 at 09:18 PM

 theVariable = fromObject.GetComponent<fromScript>().myVariable;

And you could change the Start() function in fromScript to an Awake() function, to make sure myVariable is set to true before the TO Script gets that value.

GetComponent<T>() is a function that returns a reference to the fromScript. Then you can access its public variables with a dot separating the reference name and the variable name. For example,

 var otherScript : fromScript;
 otherScript = fromObject.GetComponent<fromScript>();
 theVariable = otherScript.myVariable;

does the same as

 theVariable = fromObject.GetComponent<fromScript>().myVariable;
Comment
Add comment · Show 4 · 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 Edyvargas · Oct 14, 2016 at 01:19 AM 1
Share

Great, thank you very much, now works perfect (seting the value of the variable on the Awake() function as you mention), just one question, in the actual code the result of the myVariable from the fromScript its in the Update() function, cause is the result of a button pressed, but then the TO Script doesnt reads it, cause it reads false even when the myVariable its changed to true, how can i do to make the TO Script reads this value properly?, thanks again!

avatar image Edyvargas Edyvargas · Oct 14, 2016 at 04:12 AM 0
Share

Hi again, ok ist Solved, i just place the:

      theVariable = fromObject.GetComponent<fromScript>().myVariable;

from the TO Script, into the Update() function ins$$anonymous$$d of the Start() function used before, and now it "updates" correctly :), thanks.

avatar image ElijahShadbolt Edyvargas · Oct 14, 2016 at 04:26 AM 0
Share

That's totally fine!

Yeah, if you want two value variables (like bool) to be the same, you have to update the other one every time you change the first one.

avatar image Edyvargas ElijahShadbolt · Oct 14, 2016 at 09:13 PM 0
Share

Great, thanks.

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

73 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 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 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 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 avatar image

Related Questions

How can I improve my trading script? 1 Answer

non-static Variable in script on multiple gameObjects reset. Why? 1 Answer

How do I attach a GameObject to a static variable? 1 Answer

Value increase or decrease smoothed 1 Answer

Using a variable value with GetComponent 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