weird behaviour of input.getbuttondown

Hi all,

I would like to ask for your help on the following question. I have an environment with avatar. In the environment there are several similar objects (let’s say small boxes), that avatar can collide with. I want that once the player is colliding with one of these objects and user clicked certain button the object became selected. And then when after awhile to click the same button to deselect object. I am doing that in OnTriggerStay function (script is added on selectable object).

var objectSelected = false;

function OnTriggerStay (col : Collider)
{
var playerType : ThirdPersonStatus = col.GetComponent(ThirdPersonStatus);
if (Input.GetButtonDown (“Fire3”))
if (objectSelected)
{
Debug.Log (“object is deselected”);
objectSelected = false;
}
else
{
Debug.Log (“object is selected”);
objectSelected = true;
}
}
}

So now when I am standing on my object and click Fire3 it prints out selected and deselected several times.
My question is how to make it that on the button click it reacts just once?

Thanks for your help

You can make like that:

This is the more easy way and work fine.

var objectSelected = false;

function OnTriggerStay (col : Collider) 
{
     var playerType : ThirdPersonStatus = col.GetComponent(ThirdPersonStatus);
     if (Input.GetButtonDown ("Fire3")) {     
          if (objectSelected)
          {
               Debug.Log ("object is deselected");
               objectSelected = false;
          }
          else
          {
              Debug.Log ("object is selected");
              objectSelected = true;
          }
     }
     
      // Here the code will wait a half second to test again the code
     yield WaitForSeconds(0.5);
}