“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)
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
}
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
}
}