Keyboard Input Question

I'm testing out a script where by when the player presses a key "1" on the keyboard, it will disable a component. The script is as follow.

var c1 : GameObject;

var c1Boo : boolean = false;

function Update()
{
    if(Input.GetKey("1")
    {
        if(!c1Boo)
        {
            c1Boo = true;
            CheckBoo();
        }

        else if (c1Boo)
        {
            c1Boo = false;
            CheckBoo();
        }
    }
}

function CheckBoo()
{
    if(c1Boo)
    {
        c1.GetComponent("MoveCube").enabled = false;
    }

    else if(!c1Boo)
    {
        c1.GetComponent("MoveCube").enabled = true;
    }
}

Correct me if my logic is wrong. In my Update(), if the player presses the key "1", it will check for the first condition which the c1Boo is false. Then it will turn c1Boo to true and activate CheckBoo() which will check for c1Boo condition. If that condition is true, c1 will be disabled. The CheckBoo should only run when the player presses "1". But I did a Debug.Log() and it shows that the CheckBoo is running for a few more frames before it stops.

When I used an OnGUI(), making the control a button, I don't face this problem. The gameObject is disabled when the player first clicks it and will be enabled when the player clicks again.

Your problem stems from the fact that you are using Input.GetKey instead of Input.GetKeyDown or Input.GetKeyUp.

Your entire script could be condensed to this:

var moveCube : MoveCube;

function Update() {
    if (Input.GetKeyDown("1")) moveCube.enabled = !moveCube.enabled;
}

If you feel the need to use GetComponent, first, don't use a string if you can help it, and use the Generic version. The way you had it is unnecessarily slow. (Using a reference to the MoveScript, as in the previous code snippet, is faster still, than this following snippet):

c1.GetComponent.<MoveCube>().enabled