Enabling a controls menu

I am making a code that enables a controls menu when M is held.
However, I can’t figure out how to do it, and I’m getting some errors.

This is my code:

using UnityEngine;

public class ControlsMenu : MonoBehaviour
{
    
    public GameObject Menu;

    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey(KeyCode.M))
        {
            Menu.Enable;
        }
    }
}

Where did you get that GameObject has Enable method? There is only SetActive(), and you can’t call any method without (), only C# Properties work that way

Im sure you have found your code doesnt compile. What you were looking for is Menu.SetActive(true);

One thing you might want to consider is that if you press M currently that doesnt just run once. You can prove this to yourself by adding a Debug.Log("M was pressed"); inside the if and watching the console. Even if your console is in collapsed mode you will see either many lines or one line with some numbers at the end.

Why does it matter, well lets say you wanted M to toggle something, each frame it would turn on and off at this point. That would then give you random outcome.

What you really wanted was Input.GetKeyDown(Keycode.M) this detects the first time it goes down.. So why are there two? Well if you were pressing accelerate on a car, you dont want it to only fire once, you want to accelerate while its down, so then you would add thrust while that happens, but if you were say generating a new object, if you did it once every frame, and you’'re getting 200fps, well that just maybe generated 200 objects..

I tried your suggestions, but it didn’t go as expected.

using UnityEngine;

public class ControlsMenu : MonoBehaviour
{
    
    public GameObject Menu;

    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.M))
        {
            Menu.SetActive(true);
            Debug.Log("You pressed M.");
        }
    }
}

“You pressed M” appeared in the console, but the menu was not active.

How can I fix this?

did you assign the menu? what exactly is your menu? if you enabled the gameobject in scene view does it show? are you sure its not showing but in the wrong place??

Programming is a lot about fussing over detail.

I have not mentioned yet that I’m a beginner in coding, but this is what I came up with:

else
{
     Menu.SetActive(false);
}

So now the menu automatically disabled when I start the game, even if it’s enabled in scene mode. Then it doesn’t work again when I press M.

I found out that I just had to apply the code to the canvas, not the menu itself. This keeps the menu disabled, then when I press M, it appears for just one frame.

Next, I changed the Input.GetKeyDown(Keycode.M) to GetKey so that the menu stays open if I hold M, which is my original goal.

Thank you for the help with the code.

yes if the code is on a disabled item it doesnt run things like update