Having trouble coding an on/off toggle for an animation in script. Please help!

So I have this animation I want to run, and the animation is triggered during an “OnTriggerStay”. Within that same OnTriggerStay I want to press a single key to activate the animation and then press the same key to turn it off. I tried to do that but it didnt work because it gets called on multiple times between “on” and “off” so it results in NO CHANGE. Im a newbie so help would be MUCH appreciated. Here is a snippet of the code, I used a bool to try and make it so when i pressed “A” the bool would work as a toggle.

void OnTriggerStay(Collider col)
	{
		if (Input.GetKeyDown (KeyCode.A) && offon == true)
		{
			player.GetComponent<Animator> ().SetBool ("doingAnimation",false);
			offon = false;
		}
		if (Input.GetKeyDown (KeyCode.A) && offon == false)
		{
			player.GetComponent<Animator> ().SetBool ("doingAnimation",true);
			offon = true;
		}
	}

Like I said before, with every 1 press, the animation gets set to true and false multiple times and it results in a net change of nothing. I want to press the “A” key to turn the animation OFF and then press it again to turn it ON. Please help and thank you!

Hey @Newbie100, I hope this helps you!

void OnTriggerStay(Collider col)
{
    if(keycheck && Input.GetKeyUp(KeyCode.A))
    {
        keycheck = false;
    }
    else if(!keycheck)
    { 
        if (Input.GetKeyDown(KeyCode.A) && offon == true)
        {
            player.GetComponent<Animator>().SetBool("doingAnimation", false);
            offon = false;
            keycheck = true;
        }
        if (Input.GetKeyDown(KeyCode.A) && offon == false)
        {
            player.GetComponent<Animator>().SetBool("doingAnimation", true);
            offon = true;
            keycheck = true;
        }
    }

    
}