PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, November 8, 2022

[FIXED] how do i make a pause menu in unity using events in unity vr

 November 08, 2022     c#, events, menu, pause, unity3d     No comments   

Issue

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


Solution

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.



Answered By - derHugo
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing