Has anyone got a Toggle key script?

Hi, I’ve spent countless hours trying to create a Toggle key script, I’ve even improved my programming skills alot from it, but I still cant create that toggle script, even when I look at other scripts with the “Toggle” element.
Has anyone got a script that’ll alow me to toggle a key?
For instance…

I’ve been trying to turn this into a toggle code, if (Input.GetButton(“Fire1”)){
I’ve deleted my previous attempts so i cant show you what I’ve been doing sadly :frowning:
But that’s what I’ve been working from.
I’m trying to make an Object follow me when I press the “Fire1” key, In this case the LeftMouseButton.

Here’s my entire code.

var targetTransform : Transform;		// Transform to follow
var faceForward : boolean = false;		// Match forward vector?
private var thisTransform : Transform;

function Start()
{

	// Cache component lookup at startup instead of doing this every frame
	thisTransform = transform;
}



function Update   () 
{


if (Input.GetButton("Fire1")){
     print ("Activated");

     thisTransform.position = targetTransform.position;
	
	if ( faceForward )
		thisTransform.forward = targetTransform.forward;
      

        else {
        print ("NotActivated");
        


     }

     }

     }

Dave A here’s your Version, which isn’t work either.

var targetTransform : Transform; // Transform to follow
private var thisTransform : Transform;
var Active : boolean;

function Start()
{

// Cache component lookup at startup instead of doing this every frame
thisTransform = transform;

}

function Update () {

     if (Input.GetButton("Fire1")){
     Active = !Active; // toggle this value
     if ( Active )
     thisTransform.position = targetTransform.position;
     print ("Active");
 }
    else {

   
    Active = Active; // toggle this value
     if ( Active )
     print ("NotActive");
     }

}
The Oddler here’s your suggestion.

var targetTransform : Transform;		// Transform to follow		
private var thisTransform : Transform;
var Active : boolean;

function Start()
{

	// Cache component lookup at startup instead of doing this every frame
	thisTransform = transform;
}



function Update   () {


         if (Input.GetButtonUp("Fire1")){
         Active = !Active; // toggle this value
         if ( Active ) {
         thisTransform.position = targetTransform.position;

         print ("Active");
     }
        else {
  
       
        Active = Active; // toggle this value
         if ( Active ) {
         print ("NotActive");
         }
        
        }
}}

Can anyone help me out please? The help would be greatly appreciated.
Thanks for reading.

If you want to perform an action only when the button is pressed, replace the print functions to your own functions.

var toggle : Boolean;

function Update() {
    if (Input.GetButtonDown("Fire1")) {
        toggle = !toggle;

        if (toggle) 
            print("Toggled ON");
        else
            print("Toggled OFF");
    }    
}

If you want to perform an action every update depending on the toggle value, just move the switching logic outside the input test, as shown here:

var toggle : Boolean;

function Update() {
    if (Input.GetButtonDown("Fire1"))
        toggle = !toggle;

    if (toggle) 
        print("Toggled ON"); 
    else
        print("Toggled OFF");
}

faceForware = !faceForward; // toggle this value
if ( faceForward )

I’m a bit tired, so I can’t be sure if this is what you’re looking for, but I recently finished this code that makes a certain variable true when pressed, and then false when pressed again:

function Update () {
    if(Input.GetButtonDown("Fire1") && !Active)
		Activate();

    if(Active && Input.GetButtonUp("Fire1"))
		Active = false;
    
}

function Activate () {
	yield WaitForSeconds(.1);
	Active = true;

}