• 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 Cresspresso · 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 Cresspresso 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 Cresspresso · 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Add ammo to currentAmmo variable in a different script 2 Answers

C# Access to a variable by another script attached in a gameObject in the scene. 2 Answers

how do i use component gathered from ontriggerEnter, outside of that function? 2 Answers

Reference to value type 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges