How to enable and disable a script on an game object by pressing a keyboard button?

#pragma strict

 var onoff : boolean;
 var testObject : GameObject;
 
 function Update () {
 if(Input.GetKeyDown("p")){
     if (onoff == true)
         testObject.active = true;
     if (onoff == false)
         testObject.active = false;
 }

If you just want to toggle the button then you can use some simple boolean logic.

if (Input.GetKeyDown(KeyCode.P))
{
    testObject.SetActive(!testObject.activeSelf);
}

“!” is the “not operator”. It takes a boolean value and returns the opposite. So if activeSelf is true it returns false, and if it’s false it returns true. This has the effect of toggling the value back and forth between true and false.