My Escape Button Doesn't Get Read

void Update()
    {
        if(Input.GetKeyDown(KeyCode.Escape))
        {
            Debug.Log("Esc Pressed");
            if(GameIsPaused)
            {
                Resume();
            }
            else
            {
                Pause();
            }
        }
    }

Both console and EventSystem shows no debug.

Another thing is, I have a Unity Third Person Prefab with new controls. So I couldn’t click anything on pause menu. So I switched back to InputSystemUIInputModule. What did I mess up?

After the other post from this morning, why have you not yet put a Debug.Log() at the top of Update(), or if you have, did it print anything?

For your reference, let me re-include the steps to learn how to debug your code. Debugging is not optional.

You must find a way to get the information you need in order to reason about what the problem is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

What is often happening in these cases is one of the following:

- the code you think is executing is not actually executing at all ← my money is here

  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is
  • you’re getting an error or warning and you haven’t noticed it in the console window

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as Debug.Log("Problem!",this);

If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: How To - Capturing Device Logs on iOS or this answer for Android: How To - Capturing Device Logs on Android

If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

When in doubt, print it out!™

Note: the print() function is an alias for Debug.Log() provided by the MonoBehaviour class.

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

public class PauseMenu : MonoBehaviour
{
    public static bool GameIsPaused = false;

    public GameObject pauseMenuUI;

    void Awake()
    {
        Debug.Log("Script Works");
    }

    void Start()
    {
        Debug.Log("Script Works");
    }

  

    // Update is called once per frame
    void Update()
    {
        Debug.Log("Script Works");
        if(Input.GetKeyDown(KeyCode.Escape))
        {
            Debug.Log("Esc Pressed");
            if(GameIsPaused)
            {
                Resume();
            }
            else
            {
                Pause();
            }
        }
    }

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

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

Here is my all code. I literally put debug log everywhere and still everything works fine. Update works as intended. Removing debug log from Update prints 2 logs as intended. My if function is the one not working. There is no visible reason for that to not work in core. Problem is with my inputs.

8717451--1178451--upload_2023-1-9_22-15-55.png

I tried changing pos button and name too. Didn’t work either. Tried using joystick but that didn’t work either. Tried using another key “J,K,L” and that didn’t work either. I just got this code from one of Brackey’s videos and it works perfectly fine for him. Only difference is he doesn’t have a character so he doesn’t use new Input Module.

Edit: My ESC key works perfectly fine, I can use it in Unity get my cursor out of Game window.

Have you tried using a different key in GetKeyDown, to see if it’s only a problem with the escape key?

The configuration you’ve shown there is not relevant for GetKeyDown, by the way. That’s for GetButtonDown. So, it wouldn’t have done much if you tried using different keys in the “Positive Button” field!

Did you put two of these scripts in your scene? If so then they would both detect the Escape and would cancel each other out because they both operate on the same static bool.

Is Pause() and Resume() actually executing? Is anything else setting Time.timeScale?

Put Debug.Logs in your if statement to see which branch it is taking. Use debug.Log to see the value of GameIsPaused. Use Debug.Log to see if Pause or Resume is getting called and when/how many times.

I solved it.

Yes and I couldn’t find it anywhere.
What I did was, simply selected both Input Modules from Player Settings. That just solved that.