How to make a UI box appear with a key press

This problem has probably been asked many a time, but i cannot find any useful or actual answers; so i want to ask,

How do i make a UI box (spefically my inventory UI, but that wont make any difference) appear when i press a key?
i’ve tried Input.GetKeyDown and all of that, but i cannot for the life of me get it to work, plus i cannot figure out where i should put the components + scripts.

Any help would be useful, thank you!

In this case, you would need to make a separate empty gameobject that handles your inventory controls. This would need a script that takes in your input, and then enables and disables the inventory gameobject.

Here is a sample script:


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

public class EnableUIBox : MonoBehaviour
{
    //put your ui element that you want to enable/disable in this public variable in the inspector
    public GameObject inventoryMenu;

    //you can assign this key in the inspector as your inventory key
    //the default is I key in this case
    public KeyCode inventoryToggleKey = KeyCode.I;

    private bool inventoryOpen;

    // Start is called on the first frame
    private void Start()
    {
        inventoryOpen = inventoryMenu.activeSelf;    
    }

    // Update is called once per frame
    void Update()
    {
        //if you press the inventory key, it will enable/disable the inventory menu appropriately
        if (Input.GetKeyDown(inventoryToggleKey))
        {
            if (inventoryOpen)
            {
                inventoryOpen = false;
                inventoryMenu.SetActive(false);
            }
            else
            {
                inventoryOpen = true;
                inventoryMenu.SetActive(true);
            }
        }
    }
}