The question is answered, right answer was accepted
Pause menu done exactly according to tutorial doesen't work
Greetings, I tried following the Brackeys tutorial on making a Pause menu, however; when I press play, the pause menu is already there and the game is not paused. This would be a minor bug, however pressing Esc makes the menu go away as it should, but then pressing it against doesen't bring-it-up. I have skipped the step on connecting it to the "Menu", because I can't see a reason why wouldn't the code work without it.
I normally avoid asking things on forums and on here, because I don't want to bother people as long as I can figure-things-out myself, but I have spend the last three hours looking for the problem in the code and I cannot find it. It is connected to my Canvas GameObject and I have set the event functions according to the video - I have done exactly the same, except for the menu screen and the animation.
The "Placeholder" function I have tried to delete - that doesen't fix it, but it is going to be a restart button, IF I can get it to work.
Here's the code;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class MenuScript : MonoBehaviour
{
public static bool gameIsPaused = false;
public GameObject UImenu;
private void Start()
{
gameIsPaused = false;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
if (gameIsPaused)
{
Resume();
} else
{
Pause();
}
}
}
public void Resume()
{
UImenu.SetActive(false);
Time.timeScale = 1f;
gameIsPaused = false;
}
void Pause()
{
UImenu.SetActive(true);
Time.timeScale = 0f;
gameIsPaused = true;
}
public void Placeholder()
{
}
public void QuitGame()
{
Debug.Log("Shutting Down");
Application.Quit();
}
}
Another small thing - my shadow on the text doesen't work, probably because I am using TextMeshPro? Can somebody confirm?
Thanks for the help, if somebody sees this. It is either an easy fix and I am just too tired, or it is something out of my understanding. I am using Unity 2021.1.3f1
Answer by markythemurloc · May 07 at 11:09 PM
Hey, is the script attached to the pause menu that you turn on an off? If so, once the you setActive(false) the gameobject (pause canvas) it wont read your script anymore, hence you're not being able to turn it on once its off.
Yea, it is. So the solution would be to, say, attach the script to the Canvas and make it only turn on/off something lower in the hierarchy?
EDIT: YUP! That was it. Thanks a lot!
Answer by lvskiprof · May 07 at 08:25 PM
Hard to say without seeing how you set up your Pause menu object in the Editor, but I am guessing you have it Enabled, which is why you see it at the start. Set it to Enabled only when you need to actually edit it, but disable it in the Editor before you run it so it starts as Disabled.
The problem is, it will not come back up. The script is attached to the Canvas of the menu - just like in the tutorial and if I disable it, the script doesen't work.
Or it just may be, that Esc doesen't work for some reason, since the menu refuses to turn back on. It only turns things off for some reason, but according to the code, it should turn it back on.