- Home /
Editting a static variable?
ok, so to cut straight to the chase; I have 3 scripts. Script A is just a list of variables, Script B is a modified FPS walker script, and script c is a variable altering script. Script A's variables are static so that Script C can access them to change its own variables Script C's Variables are static so script B can access them. Problem: Script B IS accessing script C's variables, but they stay the basic numbers I defined, which means C isn't editting those variables. a snippet of C is to follow
static var normSpeed = 6.0;
static var sprintSpeed = 10.0;
static var aimSpeed = 2.0;
static var sprintDuration = 15;
static var gravity = 20;
static var penetrateVal = 1;
static var standardSpread = .4;
function Start () {}
function update () {}
function olympian() {
Trainer = gameObject.GetComponent("TrainMana");
if(Trainer.olympian < 2) {
sprintDuration = 45;
}
else if(Trainer.olympian < 3) {
normSpeed = 7.5;
sprintSpeed = 12.5;
aimSpeed = 2.5;
}
else if(Trainer.olympian < 4) {
normSpeed = 9.0;
sprintSpeed = 15.0;
aimSpeed = 3;
}
else if(Trainer.olympian < 5) {
normSpeed = 10.5;
sprintSpeed = 17.5;
aimSpeed = 3.5;
}
else if(Trainer.olympian > 5) {
normSpeed = 12.0;
sprintSpeed = 20.0;
aimSpeed = 5.0;
}
}
Where are all of these variables (normSpeed etc) declared?
Updated to show from top down to the bottom of the Olympian function
Answer by Eric5h5 · Nov 30, 2011 at 04:28 AM
There's no reason for those variables to be static. Just make them normal public non-static variables. The purpose of static variables isn't "so other scripts can access them" (that's what public variables are for); rather, a static variable means there is only one instance of it (and this is independent from its public/private state). Read this: http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Game_Objects.html
Thank you, after your post I did some fiddling and now everything works just fine. I appreciate your help
But in order to access the 'class.method' from another script the class or method has to be static. Otherwise the script has to instantiated in the calling script. Cant have it both ways. I want to use the class.method() way and am battling this complexity that is beyond all understanding.