Hidden boolean script not working properly

After I press the c key to crouch and I’m within the box collider of the enemy apc I don’t want the health to go down as I would be “hidden”, it works. And if I start the game and walk to the apc without crouching or pressing c once my health goes down so that works as well.

But if I press c once at the beginning when I’m not within the enemy collider and then move towards the apc it still says “player is hidden” when I’m not hidden, all i did was press c once at the beginning. In other words if I press c once I’m always hidden but I only want to be hidden when I’m close to the apc.

Here is my script (I tried adding an activation boolean variable so only if hidden is true AND I’m in box collider I shoudl get a message displaying hidden but it’s not working or updating

var health : float = 5.0;
var spawn : boolean = false; 
var activation : boolean = false;


function OnTriggerEnter (player : Collider) {
activation = true;
Debug.Log("acivation is" + activation);


if (player.gameObject.CompareTag ("Player"))

{

  var playerScript : run = player.GetComponent(run);

  if (playerScript.hidden == true  activation == true)

  {
    Debug.Log("apc has not detected you");
  }
  
  else
  {
  health = health - 1;
  Debug.Log("apc has detected you " + health);
  spawn = true; 
  }

}
}



function LateUpdate()
{
if (spawn) //if player dies reset back all the player info 
{
//gameObject.Find("fps").transform.position = Vector3(150,80,160);
health = 5.0;
spawn = false;
hidden = false;
}

}

and I’m calling the hidden from another script called run here

if (Input.GetKey("c")){ // press C to crouch
        h = 0.5 * height;
        speed = crchSpeed; // slow down when crouching
        hidden = true;

It looks like you’re not setting the “hidden” variable back to false. I see you have a “hidden = false” in LateUpdate, but I assume that should be “playerScript.hidden”.

However, rather than resetting the hidden var in LateUpdate, why not simply reset it in playerScript - in the sampe place where you originally set it?

So, something like this:

if (Input.GetKey("c"))
{
   hidden = true;
}
else
{
   hidden = false;
}

or, better yet, this:

hidden = Input.GetKey("c");

That way, as soon as you’re no longer holding “c”, the variable is set to false. Likewise, I’d probably reset the “activation” flag in an OnTriggerExit method instead of resetting it in LateUpdate.

Thank you very much, the else statement worked, I realsied after I posted this thread and sovled it like this :wink:

if (Input.GetKey(“c”)){ // press C to crouch
h = 0.5 * height;
speed = crchSpeed; // slow down when crouching
hidden = true;
a = true;

}

else if (a ==true)
{
hidden = false;
Debug.Log(hidden);
}

Thanks so much again :wink: