unity t-flip flop

C#
private bool onoff = false;

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            onoff = !onoff; // toggles onoff at each click

            if (onoff)
            {
                print("left");
            }
            else
            {
                print("right");
            }
        }
Source

Also in C#: