Search Unity

  1. Get the latest news, tutorials and offers directly to your inbox with our newsletters. Sign up now.
    Dismiss Notice

Pause Menu pops every time a new scene loads

Discussion in 'Visual Scripting' started by ToniGames69, Apr 13, 2021.

  1. ToniGames69

    ToniGames69

    Joined:
    Aug 22, 2020
    Posts:
    2
    Hi everyone, I have a really strange bug in my code. So I did a Pause Menu for my game and every time a new level loads and even when I press "PLAY" from the main menu, in the first level the pause menu pops, and it does every time the player gets on a new level.
    PS: The pause menu buttons and everything is working well, the only bug is that it pops up every new scene is loaded. Helps me plsss >.< if someone had this bug I would be forever thankful for the help
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class PauseMenu : MonoBehaviour
    7. {
    8.     public static bool GameIsPaused = false;
    9.  
    10.     public GameObject pauseMenuUI;
    11.  
    12.     void Update()
    13.     {
    14.         if (Input.GetKeyDown(KeyCode.Escape))
    15.         {
    16.             if (GameIsPaused)
    17.             {
    18.                 Resume();
    19.             }
    20.             else
    21.             {
    22.                 Pause();
    23.             }
    24.         }
    25.     }
    26.  
    27.     public void Resume ()
    28.     {
    29.         pauseMenuUI.SetActive(false);
    30.         Time.timeScale = 1f;
    31.         GameIsPaused = false;
    32.     }
    33.     void Pause ()
    34.     {
    35.         pauseMenuUI.SetActive(true);
    36.         Time.timeScale = 0f;
    37.         GameIsPaused = true;
    38.     }
    39.  
    40.     public void LoadMenu()
    41.  
    42.     {
    43.         Time.timeScale = 1f;
    44.         SceneManager.LoadScene("Menu");
    45.     }
    46.  
    47.     public void QuitGame()
    48.     {
    49.         Debug.Log("Quitting game...");
    50.         Application.Quit();
    51.     }
    52. }
     
  2. PanthenEye

    PanthenEye

    Joined:
    Oct 14, 2013
    Posts:
    136
    1. Wrong subforum, this is for Unity Visual Scripting tool (also known as Bolt).
    2. Your script does not indicate something happening on scene load. Which means that either your pause menu is enabled by default in scene, so that's the default state, or another script is enabling it.
     
  3. ToniGames69

    ToniGames69

    Joined:
    Aug 22, 2020
    Posts:
    2
    I am sorry, I am new to programming and I wasn't sure which subforum should I use for this problem. How should I modify the script to tell the pause menu to appear only when I press ESC? I am new and I don t know what i have to do.. I tried so many things but still doesn't work. After i solve my problem i will delete this thread bcs it s on the wrong subforum, i am sorry again.
     
  4. PanthenEye

    PanthenEye

    Joined:
    Oct 14, 2013
    Posts:
    136
    It's hard to tell what the actual issue is, but I'm guessing you need to disable the pauseMenuUI GameObject in the scene's Hierarchy so it's not visible before entering play mode. Then Esc would enable it. Or, alternatively, run your Resume() method in Start() which would also disable the pause menu when entering a scene.
     
unityunity