Mouse won't toggle off

I’m trying to make a game and when you press escape, the pause menu comes up and your mouse becomes visble, if you press tab, your inventory becomes visible and so does your mouse. I’ve gotten everything working except for two things, when I try to press tab my mouse just flickers on and off once really quickly, and also when I’m using the pause menu, sometimes the script becomes confused and turns my mouse on when it’s supposed to be off or turns my mouse off when it’s supposed to be off. Here is my script:

public Transform canvas;
public Transform canvas2;
public Transform player;
public Transform pauseMenu;
public Transform soundsMenu;
public Transform videoSettingsMenu;
public Transform controlsMenu;
public Transform VideoButton;
public Transform ControlsButton;
public Transform SoundsButton;
public Transform ResumeButton;
public Transform QuitButton;
public Transform BackButton;
public Transform SaveButton;
SaveGame saveGame;

void Update () 
{
	if (Input.GetKeyDown(KeyCode.Escape))
	{
        Pause();
	}
    if(Input.GetKeyDown(KeyCode.Tab))
    {
        InvOpen();
        Cursor.visible = true;
    }
}

public void Pause()
{
	if(Input.GetKeyDown(KeyCode.Escape))
	{
		if(canvas.gameObject.activeInHierarchy == false)
		{
			if(pauseMenu.gameObject.activeInHierarchy == false)
			{
				pauseMenu.gameObject.SetActive(true);
				soundsMenu.gameObject.SetActive(false);
				videoSettingsMenu.gameObject.SetActive(false);
				controlsMenu.gameObject.SetActive(false);

			}
			canvas.gameObject.SetActive(true);
			Time.timeScale = 0;
			player.GetComponent<FirstPersonController>().enabled = false;
			saveGame = gameObject.GetComponent<SaveGame>();
			saveGame.SaveGameSettings(false);
		}
		else if(soundsMenu.gameObject.activeInHierarchy == true || videoSettingsMenu.gameObject.activeInHierarchy == true || controlsMenu.gameObject.activeInHierarchy == true)
		{
			
		}

		else{
			canvas.gameObject.SetActive(false);
			Time.timeScale = 1;
			player.GetComponent<FirstPersonController>().enabled = true;
		}
	}
	
}

public void InvOpen()
{
    if(canvas2.gameObject.activeInHierarchy == false)
    {
        canvas2.gameObject.SetActive(true);
    }
    else
    {
        canvas2.gameObject.SetActive(false);
    }
}

public void Sounds(bool Open)
{
	if(Open)
	{
		soundsMenu.gameObject.SetActive(true);
		pauseMenu.gameObject.SetActive(false);
		videoSettingsMenu.gameObject.SetActive(false);
		controlsMenu.gameObject.SetActive(false);
		ResumeButton.gameObject.SetActive(false);
		VideoButton.gameObject.SetActive(false);
		SoundsButton.gameObject.SetActive(false);
		QuitButton.gameObject.SetActive(false);
		ControlsButton.gameObject.SetActive(false);
		BackButton.gameObject.SetActive(true);
		SaveButton.gameObject.SetActive(false);
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            Cursor.visible = true;
        }
    }
	if(!Open)
	{
		soundsMenu.gameObject.SetActive(false);
		pauseMenu.gameObject.SetActive(true);
		BackButton.gameObject.SetActive(false);
		SaveButton.gameObject.SetActive(true);
	}
}

public void VideoSettings(bool Open)
{
	if(Open)
	{
		videoSettingsMenu.gameObject.SetActive(true);
		pauseMenu.gameObject.SetActive(false);
		soundsMenu.gameObject.SetActive(false);
		controlsMenu.gameObject.SetActive(false);
		ResumeButton.gameObject.SetActive(false);
		VideoButton.gameObject.SetActive(false);
		SoundsButton.gameObject.SetActive(false);
		QuitButton.gameObject.SetActive(false);
		ControlsButton.gameObject.SetActive(false);
		BackButton.gameObject.SetActive(true);
		SaveButton.gameObject.SetActive(false);
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            Cursor.visible = true;
        }

    }
	if(!Open)
	{
		videoSettingsMenu.gameObject.SetActive(false);
		pauseMenu.gameObject.SetActive(true);
		BackButton.gameObject.SetActive(false);
		SaveButton.gameObject.SetActive(true);
	}
}

public void Controls(bool Open)
{
	if(Open)
	{
		controlsMenu.gameObject.SetActive(true);
		pauseMenu.gameObject.SetActive(false);
		soundsMenu.gameObject.SetActive(false);
		videoSettingsMenu.gameObject.SetActive(false);
		ResumeButton.gameObject.SetActive(false);
		VideoButton.gameObject.SetActive(false);
		SoundsButton.gameObject.SetActive(false);
		QuitButton.gameObject.SetActive(false);
		ControlsButton.gameObject.SetActive(false);
		BackButton.gameObject.SetActive(true);
		SaveButton.gameObject.SetActive(false);
        if(Input.GetKeyDown(KeyCode.Escape))
        {
            Cursor.visible = true;
        }
	}
	if(!Open)
	{
		controlsMenu.gameObject.SetActive(false);
		pauseMenu.gameObject.SetActive(true);
		BackButton.gameObject.SetActive(false);
		SaveButton.gameObject.SetActive(true);
	}
}

public void Resume()
{
	canvas.gameObject.SetActive(false);
	Time.timeScale = 1;
	player.GetComponent<FirstPersonController>().enabled = true;
	Cursor.visible = false;
	Cursor.lockState = CursorLockMode.Locked;
}
public void Back()
{
	controlsMenu.gameObject.SetActive(false);
	pauseMenu.gameObject.SetActive(true);
	soundsMenu.gameObject.SetActive(false);
	videoSettingsMenu.gameObject.SetActive(false);
	ResumeButton.gameObject.SetActive(true);
	VideoButton.gameObject.SetActive(true);
	SoundsButton.gameObject.SetActive(true);
	QuitButton.gameObject.SetActive(true);
	ControlsButton.gameObject.SetActive(true);
	BackButton.gameObject.SetActive(false);
	SaveButton.gameObject.SetActive(true);
}

Input.GetKeyDown returns true when called in the frame when the user has pressed the key. No matter how many times it is called, if it is called in the same Update that the user has pressed it, it will return true. I say that because it seems you’re not taking that into account.

On the other hand, the code is not doing what you say it is doing.

when you press escape, the pause menu comes up and your mouse becomes visble

I don’t see, in the provided code, that when pressing ESC the cursor becomes visible.

Then, I see the visibility of the cursor is being controlled by methods that are never called in the code provided. When are Sounds, Resume, etc. being called? That’s, probably, where the error is.