OnMouse menu

I have an idea for a menu where when you click on a button a bunch of other buttons appears:

private void OnMouseOver()
    {
        if (Input.GetMouseButtonDown(0) && !EventSystem.current.IsPointerOverGameObject())
        {
            //Do stuff
        } 
    }

But I want it so that if you click again outside of the buttons the menu closes. I’ve tried:

private void OnMouseExit()
        {
            if (Input.GetMouseButtonDown(0))
            {
                //Do stuff
            } 
        }

But this doesn’t work as it only registers when the mouse leaves the object and nothing else.

I think you need to store a state somewhere. This can be done simply with a bool:

bool isMouseOver = false;

void OnMouseEnter()
{
    isMouseOver = true;
}
void OnMouseExit()
{
    isMouseOver = false;
}
void Update()
{
    if(!isMouseOver)
    {
        //Mouse is ooff the object
    }
}

private void OnMouseExit()
{
isMouseOver = false;
}

    private void OnMouseOver()
        {
    if (Input.GetMouseButtonDown(0) && !EventSystem.current.IsPointerOverGameObject())
            {
                isMouseOver = true;
                //Enable buttons
            }
            else if (Input.GetMouseButtonDown(0) && !isMouseOver)
            {
                //Disable buttons
            }
    }