Hey !
I have a Canvas with two different panels. One for Menu another for a Shop with their own Scripts.
If I press ESC it goes to Menu. If I press S it goes to Shop.
However, when I am in the shop and I press ESC it still is in the shop but now it retains the script of the " Menu" and vice versa for the Shop.
How do I stop this from happening? Thank you.
The script for Menu and Shop are almost identical.
@filipemfcl Here is my “Pause Game Menu” script. It is quite long, but it shows you how you can turn on & off different sub-menu panels. I hope this helps!
using UnityEngine;
using UnityEngine.SceneManagement;
public class PauseGameMenu : MonoBehaviour
{
[Header("Main Game Music")]
public AudioSource mainGameMusic;
[Header("Pause Menu Music")]
public AudioSource pauseMenuMusic;
[Header("ButtonPressSound")]
public AudioSource pressButtonSound;
[Header("Is the PauseGameMenu Open?")]
public bool pauseGameMenu = false;
[Header("PauseGameMenu in Canvas")]
public GameObject pauseMenu;
[Header("Turn OFF the Player")]
public GameObject thePlayer;
[Header("Player Name Display Off")]
public GameObject playerNameDisplay;
[Header("Turn OFF Save Position")]
public GameObject savePositionButton;
[Header("Turn OFF Load Position")]
public GameObject loadPositionButton;
[Header("Items Panel in Canvas")]
public GameObject itemPanel;
[Header("House Items Panel in Canvas")]
public GameObject itemHousePanel;
[Header("Pet Items Panel in Canvas")]
public GameObject itemPetPanel;
[Header("Player Items Panel in Canvas")]
public GameObject itemPlayerPanel;
[Header("Current Quests Panel in Canvas")]
public GameObject currentQuestsPanel;
[Header("All Quests Panel in Canvas")]
public GameObject allQuestsPanel;
[Header("Q001 Info Button in Canvas")]
public GameObject Q001PickFlowerSwordButton;
[Header("Q002 Info Panel in Canvas")]
public GameObject Q002KillTheSpidersButton;
[Header("Q003 Info Panel in Canvas")]
public GameObject Q003CatchHurtButterflyButton;
[Header("Q001 Info Panel in Canvas")]
public GameObject Q001InfoPanel;
[Header("Q002 Info Panel in Canvas")]
public GameObject Q002InfoPanel;
[Header("Q003 Info Panel in Canvas")]
public GameObject Q003InfoPanel;
[Header("Stats Panel in Canvas")]
public GameObject statPanel;
// Update is called once per frame
void Update()
{
if (Input.GetButtonDown("Cancel"))
{
if (pauseGameMenu == false)
{
pressButtonSound.Play();
pauseMenuMusic.Play();
mainGameMusic.Pause();
Time.timeScale = 0;
pauseGameMenu = true;
Cursor.visible = true;
pauseMenu.SetActive(true);
thePlayer.GetComponent<CubeMovement>().enabled = false;
playerNameDisplay.SetActive(false);
savePositionButton.SetActive(false);
loadPositionButton.SetActive(false);
}
else
{
pauseMenu.SetActive(false);
mainGameMusic.UnPause();
pauseMenuMusic.Pause();
Cursor.visible = true;
pauseGameMenu = false;
Time.timeScale = 1;
thePlayer.GetComponent<CubeMovement>().enabled = true;
playerNameDisplay.SetActive(true);
savePositionButton.SetActive(true);
loadPositionButton.SetActive(true);
}
}
}
// All Buttons Go Here
//Main "Items Button" on far left
public void ShowItem()
{
itemPanel.SetActive(true);
itemHousePanel.SetActive(false);
itemPetPanel.SetActive(false);
itemPlayerPanel.SetActive(false);
currentQuestsPanel.SetActive(false);
allQuestsPanel.SetActive(false);
Q001PickFlowerSwordButton.SetActive(false);
Q002KillTheSpidersButton.SetActive(false);
Q003CatchHurtButterflyButton.SetActive(false);
Q001InfoPanel.SetActive(false);
Q002InfoPanel.SetActive(false);
Q003InfoPanel.SetActive(false);
statPanel.SetActive(false);
}
//House Items Button within the Main Items Button
public void ShowHouseItems()
{
itemPanel.SetActive(false);
itemHousePanel.SetActive(true);
itemPetPanel.SetActive(false);
itemPlayerPanel.SetActive(false);
currentQuestsPanel.SetActive(false);
allQuestsPanel.SetActive(false);
Q001PickFlowerSwordButton.SetActive(false);
Q002KillTheSpidersButton.SetActive(false);
Q003CatchHurtButterflyButton.SetActive(false);
Q001InfoPanel.SetActive(false);
Q002InfoPanel.SetActive(false);
Q003InfoPanel.SetActive(false);
statPanel.SetActive(false);
}
//Pet Items Button within the Main Items Button
public void ShowPetItems()
{
itemPanel.SetActive(false);
itemHousePanel.SetActive(false);
itemPetPanel.SetActive(true);
itemPlayerPanel.SetActive(false);
currentQuestsPanel.SetActive(false);
allQuestsPanel.SetActive(false);
Q001PickFlowerSwordButton.SetActive(false);
Q002KillTheSpidersButton.SetActive(false);
Q003CatchHurtButterflyButton.SetActive(false);
Q001InfoPanel.SetActive(false);
Q002InfoPanel.SetActive(false);
Q003InfoPanel.SetActive(false);
statPanel.SetActive(false);
}
//Player Items Button within the Main Items Button
public void ShowPlayerItems()
{
itemPanel.SetActive(false);
itemHousePanel.SetActive(false);
itemPetPanel.SetActive(false);
itemPlayerPanel.SetActive(true);
currentQuestsPanel.SetActive(false);
allQuestsPanel.SetActive(false);
Q001PickFlowerSwordButton.SetActive(false);
Q002KillTheSpidersButton.SetActive(false);
Q003CatchHurtButterflyButton.SetActive(false);
Q001InfoPanel.SetActive(false);
Q002InfoPanel.SetActive(false);
Q003InfoPanel.SetActive(false);
statPanel.SetActive(false);
}
public void ShowCurrentQuest()
{
itemPanel.SetActive(false);
itemHousePanel.SetActive(false);
itemPetPanel.SetActive(false);
itemPlayerPanel.SetActive(false);
currentQuestsPanel.SetActive(true);
allQuestsPanel.SetActive(false);
Q001PickFlowerSwordButton.SetActive(false);
Q002KillTheSpidersButton.SetActive(false);
Q003CatchHurtButterflyButton.SetActive(false);
Q001InfoPanel.SetActive(false);
Q002InfoPanel.SetActive(false);
Q003InfoPanel.SetActive(false);
statPanel.SetActive(false);
}
public void ShowAllQuestsList()
{
itemPanel.SetActive(false);
itemHousePanel.SetActive(false);
itemPetPanel.SetActive(false);
itemPlayerPanel.SetActive(false);
currentQuestsPanel.SetActive(false);
allQuestsPanel.SetActive(true);
Q001PickFlowerSwordButton.SetActive(true);
Q002KillTheSpidersButton.SetActive(true);
Q003CatchHurtButterflyButton.SetActive(true);
Q001InfoPanel.SetActive(false);
Q002InfoPanel.SetActive(false);
Q003InfoPanel.SetActive(false);
statPanel.SetActive(false);
}
public void ShowQ001InfoPanel()
{
itemPanel.SetActive(false);
itemHousePanel.SetActive(false);
itemPetPanel.SetActive(false);
itemPlayerPanel.SetActive(false);
currentQuestsPanel.SetActive(false);
allQuestsPanel.SetActive(false);
Q001PickFlowerSwordButton.SetActive(false);
Q002KillTheSpidersButton.SetActive(false);
Q003CatchHurtButterflyButton.SetActive(false);
Q001InfoPanel.SetActive(true);
Q002InfoPanel.SetActive(false);
Q003InfoPanel.SetActive(false);
statPanel.SetActive(false);
}
public void ShowQ002InfoPanel()
{
itemPanel.SetActive(false);
itemHousePanel.SetActive(false);
itemPetPanel.SetActive(false);
itemPlayerPanel.SetActive(false);
currentQuestsPanel.SetActive(false);
allQuestsPanel.SetActive(false);
Q001PickFlowerSwordButton.SetActive(false);
Q002KillTheSpidersButton.SetActive(false);
Q003CatchHurtButterflyButton.SetActive(false);
Q001InfoPanel.SetActive(false);
Q002InfoPanel.SetActive(true);
Q003InfoPanel.SetActive(false);
statPanel.SetActive(false);
}
public void ShowQ003InfoPanel()
{
itemPanel.SetActive(false);
itemHousePanel.SetActive(false);
itemPetPanel.SetActive(false);
itemPlayerPanel.SetActive(false);
currentQuestsPanel.SetActive(false);
allQuestsPanel.SetActive(false);
Q001PickFlowerSwordButton.SetActive(false);
Q002KillTheSpidersButton.SetActive(false);
Q003CatchHurtButterflyButton.SetActive(false);
Q001InfoPanel.SetActive(false);
Q002InfoPanel.SetActive(false);
Q003InfoPanel.SetActive(true);
statPanel.SetActive(false);
}
public void ShowStat()
{
itemPanel.SetActive(false);
itemHousePanel.SetActive(false);
itemPetPanel.SetActive(false);
itemPlayerPanel.SetActive(false);
currentQuestsPanel.SetActive(false);
allQuestsPanel.SetActive(false);
Q001InfoPanel.SetActive(false);
Q002InfoPanel.SetActive(false);
Q003InfoPanel.SetActive(false);
statPanel.SetActive(true);
}
public void ResumeGame()
{
pauseMenu.SetActive(false);
pressButtonSound.Play();
pauseMenuMusic.Pause();
mainGameMusic.UnPause();
Cursor.visible = true;
pauseGameMenu = false;
Time.timeScale = 1;
thePlayer.GetComponent<CubeMovement>().enabled = true;
playerNameDisplay.SetActive(true);
savePositionButton.SetActive(true);
loadPositionButton.SetActive(true);
}
public void RestartLevel()
{
pauseMenu.SetActive(false);
pressButtonSound.Play();
Cursor.visible = true;
pauseGameMenu = false;
Time.timeScale = 1;
thePlayer.GetComponent<CubeMovement>().enabled = true;
playerNameDisplay.SetActive(true);
savePositionButton.SetActive(true);
loadPositionButton.SetActive(true);
SceneManager.LoadScene(1);
}
public void QuitToMenu()
{
pauseMenuMusic.Pause();
pauseMenu.SetActive(false);
pressButtonSound.Play();
Cursor.visible = true;
pauseGameMenu = false;
Time.timeScale = 1;
thePlayer.GetComponent<CubeMovement>().enabled = true;
playerNameDisplay.SetActive(true);
savePositionButton.SetActive(true);
loadPositionButton.SetActive(true);
SceneManager.LoadScene(0);
}
}