Pause Menu Wont Pause

Im trying to make a pause menu for my Blockbreaker game. Problem is my game wont pause the paddle is still launching ball, animations moving etc. I used Brackeys method https://youtu.be/JivuXdrIHK0. Any help would be very much appreciated thanks
This is my code
‘’'using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PauseMenu : MonoBehaviour
{
public static bool GameIsPaused = false;

public GameObject pauseMenuUI;

// Update is called once per frame
void Update()
{
   if (Input.GetKeyDown(KeyCode.Escape))
    {
        if (GameIsPaused)
        {
            Resume();
        }
        else
        {
            Pause();
        }
    }
}
void Resume()
{
    pauseMenuUI.SetActive(false);
    Time.timeScale = 1f;
    GameIsPaused = false;
}
void Pause()
{
    pauseMenuUI.SetActive(true);
    Time.timeScale = 0f;
    GameIsPaused = true;
}

}
‘’’

This is probably going to be a little tricky, it should be working, Does the pause menu shows up when you press the escape key? And does it hides correctly? Which scripts are using that static variable?

the only time game freezes is if im in the start menu Hierachy press play and in the start menu. None of the other scenes will freeze if press escape. Buttons work fine and pause menu appears hides correctly no problem.

Which scripts are using that static variable ? now im lost

if i go back to start menu from another scene then pause menu will no longer freeze

Is there anyway you could upload your project so I can download and check it out? It’s kinda hard to know what exactly is going on.

For easier upload/download just make a package, just like Rick did in lesson 48.

ok thanks

is that ok ?

Access denied!
Can you make it public, please, just that file, not your entire drive.

sorry

i just tried my first build test. The score is not resetting when returning to start menu from pause menu so im just sorting it out now

I think it might be that horse animation messing things up i used this method

nope its not this code thats stopping pause menu from pausing

I’m really sorry it took so long. Good news is I found the issue.

First of all, I’ll tell you how I found the bug so you can do this on your own with your future projects, then I’ll copy-paste the changes you need to do to your code.

How I found it.
Time.timeScale is a global thing, so it should affect everything but it wasn’t. First I printed the timeScale on the paddle’s script update, I immediately noticed that something was wrong, the timeScale always printed 1 regardless of the pause menu, so I assumed there was something modifying the timeScale, the issue with this is, the bigger the project, the harder it is to find which script is making changes, but there’s a neat trick for this; if you are using Visual Studio you can select a word and then press ctrl+shift+f in your keyboard, this will open a search bar with several options, search in the entire solution (the whole project) and there it was, your GameSession script was modifying the timeScale during Update, next thing to do is to simply prevent this from happening.

The solution is not that simple tho, there are some issues with controls and timeScale, if you simply erase that Update method in your GameSession script you’ll notice that your paddle will still move, that’s because how the script is written, it will always move, you’ll also notice that if you press the right mouse button the ball won’t move but it will be launched, quite annoying if you ask me. You could take this as a challenge and try to solve it on your own since I already gave you all the info you need, so, if you want, stop reading here and try to solve it on your own, if you are already tired of this copy-paste the next blocks of code.

GameSession script

    void Update()
    {
        if (PauseMenu.GameIsPaused) { return; } //Add this line.

        Time.timeScale = gameSpeed;  
    }

Paddle script.

   void Update()
    {
        if (PauseMenu.GameIsPaused) { return; } //Add this line

        Vector2 paddlePos = new Vector2(transform.position.x, transform.position.y);
        paddlePos.x = Mathf.Clamp(GetXPos(), minX, maxX);
        transform.position = paddlePos;
    }

Ball script

    private void LaunchOnMouseClick()
    {
        //Add the second condition to this "if" statement.
        if (Input.GetMouseButtonDown(0) && !PauseMenu.GameIsPaused)
        {
            hasStarted = true;
            myRigidbody2D.velocity = new Vector2(xPush, yPush);
        }
    }

I did not erase the constant speed update in the GameSession script because you might need it later on and you are already using a static variable so why not take advantage of it. Just don’t rely too much on Brackeys, his tutorials are good, but he tends to use really bad practices, like that static.

Hope this helps, and again, sorry it took so long.

awe thanks for that i shall get to it your the best !

Lives system isn’t working correctly in WebGL. Not getting the extra life per completed level. Sometimes the opposite. I’m trying a few different settings but no luck so far

Privacy & Terms