- Home /
How to make variable public "In whole script" from update function?
If I want to make my:
"Input.GetButtonDown("Jump");" (Checked in Update function)
and
"PlayerControl = GameObject.FindWithTag("Player").GetComponent(FullShipController); (checked in Start function)
available from within whole script, what should I do?
(Example: i have Update function which checks for input and MoveShip function which... moves ship and PlaySound which plays sound when moving ship and i don't want to write same variable is different places)
Answer by spacepilot · Dec 28, 2011 at 05:01 PM
Declare the variables as private, at the beginning of your script, outside of any function. When assigning values to them inside a function, these values should get stored outside the functions but inside the script ( = "script-public").
Like this:
private var PlayerControl : [sometype];
private var Button : boolean;
function Start(){
PlayerControl = [whatever];
}
function Update(){
button = Input.GetButtonDown("Jump");
blablabla...divide shoesize by age, multiply with euler-quaternions...blablabla
}
You pushed my karma! Just got over 50 points now. Only 24 points left to reach next level. Tell me when you have another question :-D
Null Reference Exception at line 5 on unity/answers.pre.code ; Object not set to an instance that allows splicing and stuff, "whatever" unrecognized, shoesize doesn't fit
Answer by gegc · Dec 28, 2011 at 05:04 PM
The easiest way would be to declare a variable in the scope of the whole script and then update it when required, like so:
bool doJump;
FullShipController playerControl;
void Start() {
playerControl = GameObject.FindWithTag("Player").
GetComponent(FullShipController);
//Note: this will be null if the object is not player controlled.
}
void Update {
if(Input.GetButtonDown("Jump")) {
doJump=true;
} else {
doJump=false; //or set this to false when you actually do the jump
}
}