Forum begins after the advertisement:


[Part 9] Pause Menu not working

Home Forums Video Game Tutorial Series Creating a Metroidvania in Unity [Part 9] Pause Menu not working

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #13853
    Aria
    Level 13
    Participant
    Helpful?
    Up
    0
    ::
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.SceneManagement;
    
    public class GameManager : MonoBehaviour
    {
        public string transitionedFromScene;
    
        public static GameManager Instance { get; private set; }
        public Vector2 respawnPoint;
        [SerializeField] TypewriterCheck typewriter;
        [SerializeField] FadeUI pauseMenu;
        [SerializeField] float fadeTime;
        public bool gameIsPaused;
        private void Awake()
        {
            SaveData.Instance.Initialized();
            if (Instance != null && Instance != this)
            {
                Destroy(gameObject);
            }
            else
            {
                Instance = this;
            }
            DontDestroyOnLoad(gameObject);
            SaveScene();
            typewriter = FindObjectOfType<TypewriterCheck>();
        }
        private void Update()
        {
            if (Input.GetKeyDown(KeyCode.Escape) && !gameIsPaused)
            {
                pauseMenu.FadeUIIn(fadeTime);
                Time.timeScale = 0;
                gameIsPaused = true;
            }
    
        }
        public void UnPauseGame()
        {
            gameIsPaused = false;
            Time.timeScale = 1;
        }
    
        public void SaveScene()
        {
            string currenSceneName = SceneManager.GetActiveScene().name;
            SaveData.Instance.sceneNames.Add(currenSceneName);
    
        }
    
        public void RespawnPlayer()
        {
            SaveData.Instance.LoadTypewriter();
            if (SaveData.Instance.typewriterSceneName !=null)
            {
                SceneManager.LoadScene(SaveData.Instance.typewriterSceneName);
            }
            if (SaveData.Instance.typewriterPos != null)
            {
                respawnPoint = SaveData.Instance.typewriterPos;
            }
            PlayerMovement.Instance.transform.position = respawnPoint;
            StartCoroutine(UIManager.Instance.DeactivateDeathScreen());
            PlayerMovement.Instance.Respawned();
        }
    }

    Here is my Game manager
    This is what i see when i press escape in play mode


    and here is my pause menu set up

    #13856
    Joseph Tang
    Level 13
    Moderator
    Helpful?
    Up
    0
    ::

    I presume you cannot control, move or press anything in game after pressing the escape button.
    If so, it’s most likely that your game is simply paused/frozen and everything works as intended.

    The reason as to why this happens is because your GameManager.cs set’s the Time.timeScale = 0;. This essentially pauses the game.
    However, some things like the FadeUI.cs on the Pause Menu object needs time to function. Thus, having the timescale set to 0 will never allow these scripts to complete their function as their time doesn’t move forward,

    For instance, in FadeUI.cs, you likely have:

    IEnumerator FadeIn(float _seconds)
        {
            canvasGroup.alpha = 0;
    	while (canvasGroup.alpha < 1)
            {
    	    canvasGroup.alpha += Time.deltaTime / _seconds;
    	    yield return null;
    	}
    	canvasGroup.interactable = true;
    	canvasGroup.blocksRaycasts = true;
    	yield return null;
        }

    Simply swap out the code for Time.unscaledDeltaTime to fix the issue:

    IEnumerator FadeIn(float _seconds)
        {
            canvasGroup.alpha = 0;
    	while (canvasGroup.alpha < 1)
            {
                canvasGroup.alpha += Time.deltaTime / _seconds;
    	    canvasGroup.alpha += Time.unscaledDeltaTime / _seconds;
    	    yield return null;
    	}
    	canvasGroup.interactable = true;
    	canvasGroup.blocksRaycasts = true;
    	yield return null;
        }

    If this isn’t the case, can you show the Hierarchy setup and possibly manually crank up the Alpha on your Canvas Group on the Pause Menu as I can’t think of some other issue on this problem.

    As a side note, to post images you can use our “IMG” tool by pasting the image link directly and inputting (if any) alternate captions in the prompt. and to give code segments, you can use “< pre >“, and “< /pre >” instead of ” ` “.

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: