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 geennaam · Jun 07, 2012 at 07:29 PM · gameobjectvariableslink

How to link this 2 variables from 2 different scripts / gameobjects?

Hi, can someone tell me how to link this 2 scripts? I have read the Script Reference, but I don't understand it. http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Game_Objects.html

I have 2 scripts. In the first I have a variable. If I press on "k" the testVariable in the first script is changed to 5. If testVariable is 5, I need to set anotherVariable in the second script to 1. Can someone explain this to me so I can use this in my other scripts?

First script: testInput.js

 var testVariable = 1;
 
 function Update () 
 {
     if(Input.GetKey("k"))
     {
         testVariable= 5;
     }
 }

Second script: testOutput.js

 var anotherVariable = 0;
 
 function Update () 
 {
     if(testVariable= 5)
     {
         anotherVariable= 1;
     }
 }
Comment
Add comment · Show 1
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 geennaam · Jun 08, 2012 at 03:32 PM 0
Share

@Bérenger $$anonymous$$antoue, Sorry, but I don't understand your answer. Can you please make a little example with the 2 test scripts? I've been searching long for this, but I can't understand it because my English is not very well. If you can make a example I will understand.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Berenger · Jun 07, 2012 at 08:04 PM

You can have a reference of testInput inside testOutput and test testInput.testVariable. testVariable could be assigned in the inspector or with a FindXXX function.

Another way would be to have a reference of testOutput inside testInput, so you can call a function testOutput.OnTestVariableChange( testVariable ), the test being inside instead of Update.

Finally, if you're feeling brave, there is singletons.

Comment
Add comment · Show 2 · 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 Berenger · Jun 08, 2012 at 07:23 PM 0
Share

Here you go.

First script: testInput.js

 var testVariable = 1;
 var testOutput to; // Assign it in the inspector

 function Update()
 {
     if( Input.Get$$anonymous$$ey("k") )
     {
         testVariable= 5;
         to.UpdateAnotherVar( testVariable );
     }
 }

Second script: testOutput.js

 var anotherVariable = 0;

 function UpdateAnotherVar( i : int ) 
 {
     if( i == 5) // = is assignation, == is equality test
     {
        anotherVariable = 1;
     }
 }
avatar image Berenger · Jun 10, 2012 at 11:44 PM 0
Share

geennam : I got an error on the second line of testInput.js, it is expecting a ;. But it can't find it.

$$anonymous$$e : Well, that's because I code in JS like a penguin with alzheimer wearing boxing gloves. Sort of like that.

Anyway,

 var to : testOutput;

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Linking 2 script variables (min + max) to one editor bar - MinMaxSlider? 0 Answers

Link 2 variables from 2 different scripts to 3rd script 1 Answer

how can I display a variable as a GUIText 5 Answers

Help touch button 1 Answer

Destroy all GameObjects EXCEPT some... 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