- Home /
Static Variables
At first, I have read all the static variables post on forum and questions.
I am using 2 scripts - 1 that is Running and 1 that is Character Motion
In the first script I have declared a static variable called running.
#pragma strict
static var running : boolean;
In the second script I'm trying to call the variable running from the script but I don't know why it doesn't work
var running;
running = Running.running;
And I have also tried with getComponent and I got the same result.
running is suposed to be a boolean that when activated max Speed of the character will increase with 2
It would be a good idea to define running as a boolean and also to put #pragma strict at the top of all of your source files - this forces you to be clear in your variable declarations and helps fix some hard to spot bugs. It's necessary for platforms like iPhone.
static var running : boolean;
Now what error do you get when you try to access the running variable - because it looks ok apart from the declaration needing the type.
$$anonymous$$ indentifier "Running"
in Character motor script
http://s8.postimage.org/la6d27hat/Untitled.png
I have also attached both scripts to Player
Can you post the other script? Does it have anything in it called Running that could be hiding the Running class?
Answer by Fattie · Sep 22, 2012 at 08:55 AM
AMD dude, this ..
gives me the moral authority to say that the "static" path you have stumbled on to is a false path.
So, completely forget about "static". Don't use it.
If you simply post all your code clearly, someone will easily find the problem and be happy to do so!!
A) get rid of the static
B) have a variable called "running" IN ONE SCRIPT ONLY.
DO NOT HAVE A VARIABLE CALLED RUNNING IN THE OTHER "SECOND" SCRIPT.
C) in the second script do this in awake
var teste:GameObject;
teste = GameObject.FInd("you type this");
if ( teste == null )
Debug.Log("Something is messed up");
else
Debug.Log("everything is peachy");
D) test that. tell us exactly what happens
E) in the second script add code like
var otherScriptName:OtherScriptName;
otherScriptName = teste.GetComponent(OtherScriptName);
now in that second script, only ever use...
otherScriptName.running
again, see point 2 .. get rid of "running" from the second script, and from now on use only "otherScriptName.running"
F) if still having problems, delete the two paste bins, repaste everythign, and let us know
Yeah that's true - though if it can't find Running - then I doubt GetComponent(Running) will work either... Interesting.
I want a static variable because I have 2 scripts and I want to use the boolean "running" from the first scrip in Character $$anonymous$$otor script as for the player when he presses shift var running = true and if running = true the variables maxForwardSpeed and the others 2 will increase with 2. And I want to separate the running script from Character $$anonymous$$otor
$$anonymous$$agnificent!
I urge you to please not use Static variables until you are totally confident with, err, parametric polymorphism. Add, err, memorise all of the book seen in this screen shot !
http://answers.unity3d.com/questions/265828/if-you-destroy-a-list-of-class-objects.html
do not hesitate to ask more questions here but first erase all Static mentions. and don't use globals! :) bottoms up
Answer by aldonaletto · Sep 22, 2012 at 09:53 AM
The problem here is the compilation order. Unity compiles scripts in 4 consecutive steps, according to the folders where they are stored (see Script Compilation, in the docs). Scripts compiled in some step can't see the others that will be compiled in subsequent steps. Since CharacterMotor is in Standard Assets (first step), it can't see your script (third step, apparently).
You can move the running declaration to CharacterMotor, like this:
CharacterMotor.js:
static var running: boolean = false;
...
if (running == true){
maxForwardSpeed = 5 ;
maxSidewaysSpeed = 5 ;
...
Running.js:
...
function Update(){
// assign the button state directly to running (you don't need that doubled if):
CharacterMotor.running = Input.GetButtonDown("Run");
if (run == true){ //
...
NOTE: A more elegant and bullet-proof alternative would be to not make running static - being static, all characters that use CharacterMotor will run when you press Run! The non static alternative would be like this:
CharacterMotor.js:
public var running: boolean = false;
...
if (running == true){
maxForwardSpeed = 5 ;
maxSidewaysSpeed = 5 ;
...
Running.js:
... private var chMotor: CharacterMotor; // reference to CharacterMotor
function Start(){ // get the CharacterMotor assigned to this player: chMotor = GetComponent(CharacterMotor); }
function Update(){ chMotor.running = Input.GetButtonDown("Run"); if (run == true){ // ...
ouch guys. the OP is (A) struggling with basic variable and GetComponent use, and has (B) accidentally started using static ... could be a further complication to introduce compilation order! :O
I sure hope you mean $$anonymous$$ike - i'm the village drunk. I mean I just vomited on myself, seriously. Take it easy now !
always remember, program$$anonymous$$g is the hardest human challenge that has ever existed !! failure is the only option!
always remember that no software system ever - that's No software system .. Ever .. has ever been completed to a working state. if you're tackling program$$anonymous$$g you're beyond el33t. you've put yourself in the hardest human place. even the lamest lame-ass question is a masterpiece]
guys like Aldo and $$anonymous$$ike can see in to the Rubik's cube. that is way difficult - VERY rare
and now, time for chicken.
@Fattie for the classic post. I got a few laughs out of this page xD
...and... bump for a new-ish person having some problems with static's.
http://answers.unity3d.com/questions/680545/accsesing-variables-from-another-script-but-they-h.html
Your answer
![](https://koobas.hobune.stream/wayback/20220613081331im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Is Running/Playing flag? 2 Answers
I cant seem to add a cooldown to this script 1 Answer
Game not working properly 0 Answers