- Home /
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;
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);
}
this works but now when I reload the scenes the values of the clock isn't reset D: Is there a way around this????
$$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
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.
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.
Your answer
Follow this Question
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