Boolean check not referenced correctly from other script

Hello - in my game the player can switch between the first person controller and the third person controller on pressing the space bar.

On hitting a collider however, after three seconds it is switched to the first person controller automatically (without the player pressing space)

This works - but for some reason you have to press spacebar twice in order to switch back to the third person controller. I don’t think I’m referencing the boolean check from the SwitchControllers script correctly - but I can’t work out what I’m doing wrong.

This is the ForceIntoFirstPerson script (JavaScript):

var cam01 : GameObject; // first person camera
var cam02 : GameObject; // third person camera
var player01 : GameObject; //first person controller
var player02 : GameObject; //third person controller
public static var check : boolean = false;

    
function OnTriggerEnter(other: Collider){

if (other.tag == "Player")
{
  yield WaitForSeconds (3);
   
cam01.gameObject.active = true;
cam02.gameObject.active = false;
player01.active = true;
player02.active = false;
check = true;
}

}

This is the Switch Characters script:

var cam01 : GameObject; // first person camera
    var cam02 : GameObject; // third person camera
    var player01 : GameObject; //first person controller
    var player02 : GameObject; //third person controller
    public static var check;                 //  check-variable
 
    //start with first person active
    function Start() {
    	check = true;
       cam01.gameObject.active = true; 
       cam02.gameObject.active = false; 
       player02.active = false;
       //check = true;
 
 
    }
 
 
    function Update() {
 
    player01.transform.position = player02.transform.position;
 
     if (Input.GetKeyDown ("space")) {
         if(check) {
         cam01.gameObject.active = false; 
         cam02.gameObject.active = true; 
         player01.active = false;
         player02.active = true;
       }
       else {
         cam01.gameObject.active = true; 
         cam02.gameObject.active = false; 
         player01.active = true;
         player02.active = false;
       }
    check = !check;
    }
    }

I tried this alteration (amongst many others), but it didn’t work.

if (check) check = false;
else check = true;
}

Any help would be so, so much appreciated!! (I’m stuck until I sort this one out, and deadlines are scarily close)

Thanks so much, Laurien

Well you are correct, your boolean check is not being referenced correctly.

In your ForceIntoFirstPerson script, you need to have a reference to the switchCharacters script and set the ‘check’ variable of the switchCharacters script.

I’m not entirely sure of the Javascript version, but in C# it would be something like

 switchCharacters switchScript;

   Start(){
     ...
      switchScript = GetComponent<switchCharacters>();
    }
    ...
    switchScript.check = true;

Javascript is probably:

 var switchScript: switchCharacters;

 Start(){
    switchScript = GetComponenet<switchCharacters>();
    switchScript.check= true;
 }

Your other syntax, check =!check does exactly what your alternative says.

The static variable ‘check’ in the two scripts are not the same variable, but two different. You probably mean to change SwitchCharacter’s check in both scripts. Static variables still belong to the class they’re declared in. Access them using ClassName.variableName. So in your case: SwitchCharacters.check = !SwitchCharacters.check.