I've made a Pause menu but

I made a pause menu but can not seem to get it to work properly. if the canvas is checked to display all i get is a pause menu that is constantly being triggered and untriggered really fast. Or it just doesn’t appear. Heres the code. Any help would be appreciated.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PauseMenu : MonoBehaviour
{
[SerializeField] private GameObject PauseMenuMain;

[SerializeField]  private bool isPaused;

private void Update()
{
    if (!Input.GetKeyDown(KeyCode.Escape))
    {
        isPaused = !isPaused;
    }

    if (isPaused)
    {
        ActivateMenu();
    }

    else
    {
        DeactivateMenu();
    }


}

private void ActivateMenu()
{
PauseMenuMain.SetActive(true);
}

private void DeactivateMenu()
{
    PauseMenuMain.SetActive(false);
}

}

Hi,

Have you already tried to add Debug.Logs to your code to see what is going on during runtime?

Your problem is here. When you are not pressing the escape key (which is most of the time) you are toggling between paused and not paused. Every frame

1 Like

Lmao. i didnt even notice that. thank you. i guess i was just too close to see it

I ran debug and after the adjustments its still toggling every frame.

 void Update()
    {
        if (!Input.GetKeyDown(KeyCode.Escape))
        {
            Debug.Log("is paused");

            isPaused = true;
            return;
        }
        else
        {
            isPaused = false;
        }

    }

That’s odd. Theoretically, if you press the escape key down, the else should get executed once. And in the next frame, the if-block should get executed again. GetKeyDown returns true only in the frame when you press the key down. Is this what you wanted to achieve?

With this code it will unpause for a single frame and then immediately pause again on the next frame. But then it will stay paused until you press escape again which will, again, unpause for a single frame.

Is this the only code in your update now?

Yeah. I’m really new to code so I’m not entirely sure how to fix this. Ive only been learning code for about 2 weeks.

Can you show the new, full PauseMenu script as you did above?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PauseMenu : MonoBehaviour
{
public GameObject PauseMenuMain;

 public static bool isPaused;

 void Update()
{
    if (Input.GetKeyDown(KeyCode.Escape))
    {
        Debug.Log("is paused");
        isPaused = true;
        return;
    }
    else
    {
        isPaused = false;
    }

}

void ActivateMenu()
{
    PauseMenuMain.SetActive(true);
    isPaused = true;
}

 void DeactivateMenu()
{
    PauseMenuMain.SetActive(false);
    isPaused = false;
}

}

How are you activating this?

If this script is active, it will set isPaused to false immediately because you flipped the escape check to now set isPaused to true when you press escape but that won’t do anything in this script because all it does is to set isPaused back to false. ActivateMenu() and DeactivateMenu() is no longer being called.

How do you intend the pause to work? Were you thinking the player press escape to pause the game, and then escape again to resume? Is it escape to pause and then there’s a button on the screen to resume? What is PauseMenuMain?

The idea is to have the player hit esc to pause and unpause. The PauseMenuMain is the controller of the pause canvas (an empty game object).

So, if I understand your structure you have this script somewhere that is always active (because it needs to listen for escape). When the player hits the escape key, you want to make PauseMenuMain active/inactive based on the current state.

This should do what you want.

using UnityEngine;

public class PauseMenu : MonoBehaviour
{
    public GameObject PauseMenuMain;
    public bool IsPaused = false;

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            TogglePause();
        }
    }

    private void TogglePause()
    {
        IsPaused = !IsPaused;
        PauseMenuMain.SetActive(IsPaused);
    }
}
1 Like

Thank you very much. I was just over complicating it. This script makes much more sense. It works

Your original code was close, it just had a few little bugs to fix

Thank you for taking the time to show me my errors.

I second bixarrio’s opinion. The code itself was not the problem. You had everything you needed to make your idea work, which is great considering you have learnt programming for 2 weeks. There was “just” a flaw in the logic. I’m sure you would have found the solution yourself at some point. Keep up the good work! :slight_smile:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms