I'm trying to make a pause menu in unity VR I want when I press a button on the controller the menu appears but I don't know how to make the menu appear when the button is pressed a picture of what I have om the event thing but I most likely did that wrong too
CodePudding user response:
They way you have set it up currently is that when you click the button it is calling
pauseMenu.SetActive(false);
thus it will never enable that object.
You rather need a dedicated component like e.g.
public class PauseMenu : MonoBehaviour
{
// Reference this vis the Inspector in case the PauseMenu is NOT
// the same object this component is attached to.
// Otherwise it will simply use the same object this is attached to
[SerializeField] private GameObject pauseMenu;
// Adjust this vis the Inspector
// Shall the menu initially be active or not?
[SerializeField] private bool initiallyPaused;
// Public readonly property so you can make other scripts depend on this
// e.g. do not handle User input while pause menu is open etc
public bool IsPaused => pauseMenu.activeSelf;
// Additionally provide some events yourself so other scripts
// can add callbacks and react when you enter or exit paused mode
public UnityEvent onEnterPaused;
public UnityEvent onExitPaused;
public UnityEvent<bool> onPauseStateChanged;
private void Awake ()
{
// As fallback use the same object this component is attached to
if(!pauseMenu) pauseMenu = gameObject;
SetPauseMode(initiallyPaused);
}
// This is the method you want to call vis your event instead
public void TogglePause()
{
// simply invert the active state
SetPauseMode(!IsPaused);
}
private void SetPauseMode (bool pause)
{
pauseMenu.SetActive(pause);
if(pause)
{
onEnterPaused.Invoke();
}
else
{
onExitPaused.Invoke();
}
onPauseStateChanged.Invoke(pause);
}
}
Attach this to your pause menu object and in the event reference the PauseMenu.TogglePause
method instead.