- Home /
Static Variables
I have several 'if' statements within my scripts. I would like to use the same boolean variable in all of my scripts.
How can I access a public var in one script when it was created in another?
Answer by Eric5h5 · Sep 29, 2012 at 02:08 AM
The link above applies to all types of variables, and functions too.
It doesn't matter at all what type the variable is. See the examples on that page that use GetComponent.
you just declare your (static var youNameItBoolean=true;) this in the script YouNameIt.js
and then you just say in the other script : if(YouNameIt.youNameItBoolean){ do something }
so in your main script where the variable is defined is like this
: where$$anonymous$$yBooleanIsCreated.js // this is the file name
static var myBoolean=false;
: where$$anonymous$$yBooleanIsCalled.js // this is again another js
function Update(){
if(where$$anonymous$$yBooleanIsCreated.myBoolean ==false){
//or you can write it like this :
//if(!where$$anonymous$$yBooleanIsCreated.myBoolean){
//both of them do the same
where$$anonymous$$yBooleanIsCreated.myBoolean=true;
}
}
this makes it a good example i think
this only makes the global variable from false to true
Your answer
Follow this Question
Related Questions
Accessing a js static var from a c# script 1 Answer
Cannot use Static var/ Unknown Idientifier 1 Answer
call variable from string? 1 Answer
Is there any way to edit variables inside other scripts without declaring them as static? 1 Answer
How to access a non static variable by another static variable? 1 Answer