Key pressed to wait

Hello fellow scripters. I am trying to make a script for when I click Escape, then it bings up a menu button. But when I click Escape in the game, it flickers, and if you hold escape then it will just slow down the page. I believe this is because it does not have a wait second on it or something. Here is part of the script.
void Update()
{
if (Input.GetKey(KeyCode.Escape))
{
if (GameIsPaused)

{
Resume();
}
else
{
Pause();
}
}
}

public void Resume()
{
pauseMenuUI.SetActive(false);
Time.timeScale = 1f;
GameIsPaused = false;
}

public void Pause()
{
pauseMenuUI.SetActive(true);
Time.timeScale = 0f;
GameIsPaused = true;

}
public void LoadMenu()
{
SceneManager.GetActiveScene();
}

public void QuitGame()
{
Application.Quit();
}

}

First: Use code tags.

Second: GetKey returns true every frame the key is held. You want GetKeyDown for this.

1 Like

Sorry, new to posting forums. Thanks for the quick reply.

But also, how to I set a timer of when the key will be pressed again?

What do you mean by this? Why do you want a timer?

Like when you spam Escape in non multi playe game, you can spam the button for escape sometimes and make the game way easier. I want to set it to that you can only click the Escape button every 3 seconds so it is not spammable.

Ah, ok.
In that case:

float lastEscPressed = -999f;
public float pauseSpamDelay = 3f;
void Update() {
if (Input.GetKeyDown(KeyCode.Escape)) {
if (gameIsPaused)
{
Resume();
}
else {
if (Time.time < lastEscPressed + pauseSpamDelay) {
lastEscPressed = Time.time;
Pause();
}
}

Do I add this anywhere? Sorry, new to scripting and trying to learn everything.

Yea, I tried it. It still brings u Menu and everything, but yet still no delay.

Post your current code, and don’t forget to use CODE tags.
https://discussions.unity.com/t/481379

void Update()
{
if (Input.GetKey(KeyCode.Escape))
{
if (GameIsPaused)

{
Resume();
}
else
{
Pause();
}
}
}



public void Resume()
{
pauseMenuUI.SetActive(false);
Time.timeScale = 1f;
GameIsPaused = false;
}

public void Pause()
{
pauseMenuUI.SetActive(true);
Time.timeScale = 0f;
GameIsPaused = true;

}
public void LoadMenu()
{
SceneManager.GetActiveScene();
}

public void QuitGame()
{
Application.Quit();
}

}

This is my code. I want it set to every 3 seconds, then it will register you are clicking Escape.

Use the code I posted in my last comment. It basically replaces your existing Update() function.

Tried, does not have a wait on it, altho I see a wait timer in the Properties, it doesn’t change still spammable.