Switching between controllers - why do I have to press spacebar twice before it works?

Hello - in my game the player can switch between first and third person on pressing the space bar. Once the third person controller hits a collider, after 3 seconds it switches to the first person controller automatically without the player pressing space.

The script to SwitchBetweenControllers works, and the one to ForcePlayerIntoFirstPerson works - but my problem is that once the player is in first person - he has to press the spacebar twice before switching to the third person controller.

I can’t work out why this is - any help would be so much appreciated!

This is the script for SwitchBetweenControllers (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
    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;
    }
    
    
  }

And this is the script for ForcePlayerIntoFirstPerson (which is attached to the collider):

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;
 
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;

}

 
}

Thanks so much, Laurien

OK so if they are two separate scripts, there are a couple of issues with your boolean checks.

  1. In your ForcePlayerIntoFirstPerson script, you don’t need a local variable called “check”. You need to get access to the check variable in your SwitchBetweenControllers script. So your OnTriggerEnter() should be updated to this:

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

    }

Then back in your ForcePlayerIntoFirstPerson script in your update function this maybe a bit neater:

function Update() {
    if (Input.GetKeyUp("space")) {
        if (check) check = false;
        else check = true;
    }

    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;
    }
}

See if this helps!