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 Zachary Hoffman · Mar 31, 2014 at 07:58 PM · javascriptvariables

How to access variables between scripts in unity JavaScript?

I made a game have two objects in the hierarchy panel. One called GameScreen and another called Clock I set up. Each has its own script attached. GameScreen uses game.js and the other uses clock.js.

When I win the game a popup box appears and says, "You've won!" This is caused by the game.js script. However, the clock.js is still running, so my clock is still counting down. When the time is up the clock.js makes a popup box saying, "you lose!"

This causes for a "you win" screen to pop up when you win and then later a you lose screen to appear. As you can probably guess, this is super annoying. If there was a way I could change variables in one script from another,, I could get the clock to stop when you won or I could get the game to stop when the time ran out.

Is there some way to do this??

For example here are two javascript files. I want the first one to change the variable changeMe in the second to two.

1.js:

 function start(){
 
 
 }

2.js:

 var changeMe:int;
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

3 Replies

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

Answer by alexizz · Mar 31, 2014 at 08:34 PM

hey, to access variable do this :

first scripts :

 static var test : int;

Second Script :

 function Start ()
 {
    var lol = (The name of the script where there is the static variable).(the name of the variable);
 
 }
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 Zachary Hoffman · Mar 31, 2014 at 09:02 PM 0
Share

this works but now when I reload the scenes the values of the clock isn't reset D: Is there a way around this????

avatar image Zachary Hoffman · Mar 31, 2014 at 09:08 PM 0
Share

nvm I fixed it :)

avatar image Lo0NuhtiK · Mar 31, 2014 at 09:16 PM 0
Share

$$anonymous$$arking this answer as the correct one will be misleading to new Unity3D users. Although this works, it's definitely not how you want to do things all the time.

For doing it with non-static variables, look at some of the other posts below this one, and check this other topic as well->

http://answers.unity3d.com/questions/203054/accessing-a-variable-from-another-script.html

avatar image
0

Answer by djfatsteve2 · Mar 31, 2014 at 08:35 PM

I would do something like this...

1.js

 var changeMeScript : 2.js; //this also needs to be set in inspector
     
     function Start(){
     
     changeMeScript.changeVariable();
     
     }


2.js

 var changeMe : int = 129731239786;  //change this to whatever you want...
     
     function changeVariable () {
     
     changeMe += 1273612873; //change this also to whatever...
     
     }
 



Haven't tested it officially though... So no promises.

Comment
Add comment · 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
0

Answer by Shushustorm · Mar 31, 2014 at 09:07 PM

I would do something like this:

first_script.js:

var imported_second_script : second_script; imported_second_script = GetComponent(second_script);

function Start () { // sets changeMe in second_script to 20 imported_second_script.changeMe = 20; }

second_script.js:

var changeMe : int;

I haven't tried this, though. And sorry for no syntax highlighting. It doesn't seem to work for me.

Comment
Add comment · 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

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

24 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

Related Questions

How do I access a script variable from a class defined within that script? 1 Answer

Operator '-' cannot be used ... error 1 Answer

variable scope problem 1 Answer

Can 2 scripts attached to one object communicate with each other? 2 Answers

When I try to increment a variable by 1, why does it add 10? 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