using c# script to open/close GUI window

Ive been tearing my hair out trying to solve this. Ok, lets admit the truth to the fact im a total rookie. This being said ill get to the point.

I am basically trying to get my inventory GUI window to open and close by pressing I.

My code looks like this :

using UnityEngine;
using System.Collections;

public class Inventory : MonoBehaviour
{

    void OnGUI()
    {
        // Make a background box
        GUI.Box(new Rect(10, 10, 100, 90), "Inventory");
    }
    
    private bool render = false;
    private Rect windowRect = new Rect (20, 20, 120, 50);

    public void ShowWindow() {
        render = true;
    }

    public void HideWindow() {
        render = false;
    }

    public void OnGUI() {
        if (Input.GetKey(KeyCode.I))
            ShowWindow();

              if (Input.GetKey(KeyCode.I))
            HideWindow();

        }
    }

doesnt work at all either lol. any help is greatly apprecited. Basically, I just am trying to have a GUI open up and close by pressing I. Thanks in advance for any help!

What does ‘render’ do? From the looks of things, you’re changing a variable that does nothing!

You have two ‘OnGUI’ functions declared. Don’t do that. From the looks of things, the second one should be renamed ‘Update’ (since you’re only doing things in there that should only be done in Update anyway), and then in your first OnGUI function try modifying it so that it actually checks the ‘render’ variable!

void OnGUI()
{
    if(render)
    {
        // Make a background box
        GUI.Box(new Rect(10, 10, 100, 90), "Inventory");
    }
}

If you want the ‘I’ key to toggle the box, do this:

void ToggleWindow()
{
    render = !render;
}

void Update()
{
    if(Input.GetKeyDown(KeyCode.I))
    {
        ToggleWindow();
    }
}