How to toggle a key for a car to go forward or backward?

I Have two functions, one is when I want my car to go forward, and the other to go backward, If I Want to switch between them, I Keep Pressing on the “D” key. this is my code `if(Input.GetKey(KeyCode.D)){
var direct = true;
}

if(Input.GetKey(KeyCode.R)){
var reverse = true;
}

if(direct){
goDirect();
}
else if(reverse)
{
goReverse();
};`

I Don’t want to keep holding the “D” or The “R” button to make the car go in whatever direction “Continue doing the same function as long as I’m holding the key”, I want to press once, I Pressed “D” so direct = true and the car continues to go forward unless I Press the “R” Button, It’s like ON/OFF Button ?

Instead of Input.GetKey use Input.GetKeyDown. Also don’t declare direct inside if loop, put it outside any loop.

// Declare this variable above any method
var direct : bool;

if(Input.GetKeyDown(KeyCode.D)){
    direct = true;
}

if(Input.GetKeyDown(KeyCode.R)){
    reverse = true;
}

It will work like you want.