- Home /
I made an resume button, but I don't know how to code it to close my pause menu, any tips?
This is my script for the pause. (I use C#)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Pause : MonoBehaviour
{
public GameObject PauseMenu;
public bool PauseMenuActive;
void Start()
{
PauseMenu.SetActive(false);
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
PauseMenuActive = !PauseMenuActive;
if (PauseMenuActive)
{
PauseMenuOpen();
}
if(!PauseMenuActive)
{
PauseMenuClose();
}
}
void PauseMenuOpen()
{
PauseMenu.SetActive(true);
}
public void PauseMenuClose()
{
PauseMenuActive = false;
PauseMenu.SetActive(false);
}
}
Answer by SteenPetersen · May 24, 2020 at 02:56 PM
Hi there,
The way you have coded this you are constantly calling either 'PauseMenuOpen' or 'PauseMenuClose' in every frame. This is a bit unnecessary Let's try and refactor the code a bit and perhaps we will stumble on a solution to the problem.
public class Pause : MonoBehaviour
{
[SerializeField] GameObject PauseMenu;
/// <summary>
/// returns the state of the Pause menu
/// </summary>
public bool IsPauseMenuActive
{
get { return PauseMenu.activeSelf; }
}
void Start()
{
PauseMenu.SetActive(false);
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
HandlePause();
}
private void HandlePause()
{
if (!IsPauseMenuActive)
{
PauseMenuOpen();
return;
}
PauseMenuClose();
}
void PauseMenuOpen()
{
PauseMenu.SetActive(true);
}
public void PauseMenuClose()
{
PauseMenu.SetActive(false);
}
}
I tried this code and it seemed to open and close the menu just fine, let me know if it helps.
Following this you can simply call the PauseMenuClose() function from the resume button.
i got 4 errors: Assets\Pause.cs(1,22): error CS0246: The type or namespace name '$$anonymous$$onoBehaviour' could not be found (are you missing a using directive or an assembly reference?)
Assets\Pause.cs(3,6): error CS0246: The type or namespace name 'SerializeFieldAttribute' could not be found (are you missing a using directive or an assembly reference?)
Assets\Pause.cs(3,6): error CS0246: The type or namespace name 'SerializeField' could not be found (are you missing a using directive or an assembly reference?)
Assets\Pause.cs(3,22): error CS0246: The type or namespace name 'GameObject' could not be found (are you missing a using directive or an assembly reference?)
Don't forget to include using UnityEngine; at the beginning of your code.
The errors are gone, but i can't acces the menu, but i can close it with esc
hey @jaydenvanvliet9 this is only the class I wrote but yes you need to include:
using UnityEngine;
above the class, sorry I should have been more specific. That is exactly what the error is telling you by the way so something good to learn as well:
...are you missing a using directive or an assembly reference?
Is saying that you missing that using statement
Answer by tadadosi · May 24, 2020 at 03:23 PM
You could just use a UI Button and add an event to disable your pause menu parent gameobject.
Like this: