Have a 'Torch' script toggle on and off

So far i have searched and i have found a script which is perfect for what i am doing although there is no toggle on and off. the script is a timer which after a certain amount of time, it flickers the torch then ‘R’ is pressed to reset it. I would like to have ‘F’ as a toggle on and off key, could anyone help me out telling me where that code would go?

Thanks to @alucardj who provided me with this script in his ‘slender like game tutorial’.

private var startIntensity : float = 1.85;

enum flashlightState { IsWorking, Flickering, Resetting }

var currentState : flashlightState;

private var workingTimer : float = 0.0;
private var workingTimeLimit : float = 90.0;

var minFlickerSpeed : float = 0.05;
var maxFlickerSpeed : float = 0.2;

private var flickerCounter : float = 0.0;

private var resetTimer : float = 0.0;

function Start() 
{
    workingTimeLimit = Random.Range( minWorkingTime, maxWorkingTime );

    light.enabled = true;
    startIntensity = light.intensity;

    currentState = flashlightState.IsWorking;
}

function Update() 
{
    switch( currentState )
    {
        case flashlightState.IsWorking :
           IsWorking();
        break;

        case flashlightState.Flickering :
           FlickerFlashlight();
           CheckForInput();
        break;

        case flashlightState.Resetting :
           Resetting();
        break;
    }
}

function IsWorking() 
{
    workingTimer += Time.deltaTime;

    if ( workingTimer > workingTimeLimit )
    {
       flickerCounter = Time.time + Random.Range( minFlickerSpeed, maxFlickerSpeed );

        currentState = flashlightState.Flickering;
    }
}

function FlickerFlashlight()
{
    if ( flickerCounter < Time.time )
    {
        if (light.enabled)
        {
            light.enabled = false;
        }
        else
        {
            light.enabled = true;

            light.intensity = Random.Range(minLightIntensity, maxLightIntensity);
        }

        flickerCounter = Time.time + Random.Range( minFlickerSpeed, maxFlickerSpeed );
    }
}

function CheckForInput()
{
    if (Input.GetKeyDown(KeyCode.R))
    {
        currentState = flashlightState.Resetting;
    }
}

function Resetting() 
{
    resetTimer += Time.deltaTime;

    if ( resetTimer > 0.75 )
    {
       resetTimer = 0.0;
       workingTimer = 0.0;
       workingTimeLimit = Random.Range( minWorkingTime, maxWorkingTime );
       light.enabled = true;
        light.intensity = startIntensity;
        currentState = flashlightState.IsWorking;
    }
    else if ( resetTimer > 0.65 )
    {
       light.enabled = false;
    }
    else if ( resetTimer > 0.55 )
    {
       light.enabled = true;
        light.intensity = startIntensity;
    }
    else if ( resetTimer > 0.25 )
    {
       light.enabled = false;
    }
    else if ( resetTimer > 0.15 )
    {
       light.enabled = true;
        light.intensity = startIntensity;
    }
    else if ( resetTimer > 0.05 )
    {
       light.enabled = false;
    }
}

I would modify the code to allow the CheckForInput function to be performed every Update cycle, allowing it to capture the F input key. When the F input key is detected it will toggle the light.enabled value. This would then require the logic of the switch statement be placed into the CheckForInput function in the instance when the R key is pressed. The required code changers are below.

In the Update function, simply move the CheckForInput function out of the switch, like this:

function Update() 
{
    switch( currentState )
    {
        case flashlightState.IsWorking :
           IsWorking();
        break;
 
        case flashlightState.Flickering :
           FlickerFlashlight();
        break;
 
        case flashlightState.Resetting :
           Resetting();
        break;
    }

    CheckForInput();
}

Then, in the CheckForInput function, add the logic from the Update function for the R key press and add the logic for the F key press.

Update: I had originally omitted a close parenthesis below. Thank to alucardj for pointing this out.

function CheckForInput()
{
    if (Input.GetKeyDown(KeyCode.R) && currentState == flashlightState.Flickering)
    {
        currentState = flashlightState.Resetting;
    }

    if (Input.GetKeyDown(KeyCode.F) )
    {
        light.enabled = !light.enabled;
    }
}