Reference boolean check from other script (not working)

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

    
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;
var temp : SwitchCharacters = gameObject.GetComponent(SwitchCharacters);
    temp.check = true;

}

} 

And this is the SwitchCharacters 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
    var check;                 //  check-variable
 
    //start with first person active
    function Start() {
       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;
    }
 
 
  }

Could anyone help - I just can’t work out how to solve this problem at all.

Thanks, Laurien

Hmmm… Let me see if I can give you some pointers.

Here’s the simple fix:

Make a method in SwitchCharacters called Switch(). This will perform the switch and handle the bool etc.

public void Switch()
{
  check = !check; //toggle
  cam01.gameObject.active = !check;
  cam02.gameObject.active = check;
  player01.active = !check;
  player02.active = check;
}

From SwitchCharacters call Switch():

if (Input.GetKeyDown ("space")) { //only happens once per push
  Switch();
}

Then from ForceIntoFirstPerson call Switch():

if (other.tag == "Player")
{
  yield WaitForSeconds (3);
 
  var switcher : SwitchCharacters = gameObject.GetComponent(SwitchCharacters);
  switcher.Switch();
}

Now all your functional code is in one place. This is still very basic and simplistic, but should suite your needs fine. Never have redundant code if it can be avoided.

I strongly suggest you change your variable and class names. For instance, “check” is extremely ambiguous. Instead use something like isFirstPerson or inversely isThirdPerson. This tells you something at least. Even better would be an enum with 2 options like { FIRST_PERSON, THIRD_PERSON }. Also your class names shouldn’t be verbs, generally. They are nouns, and what they do is verbs (methods). So something like PlayerCameraSwitcher, or CameraSwitchController.

Hope this helps!