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

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

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){ //
      ...