Forum begins after the advertisement:


[part 9] Unpausing malfunction

Home Forums Video Game Tutorial Series Creating a Metroidvania in Unity [part 9] Unpausing malfunction

Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #13205
    Courtmaster
    Participant
    Helpful?
    Up
    0
    ::

    When I unpause the game, the player and enemies are stuck in place running their idle animations. Upon checking the Game Manager inspector, the bool Game Is Paused remains checked. If I uncheck it, the game runs like normal.

    For clarification, the UnpauseGame() method is a public void. The Resume and Quit to Main Menu buttons both have the unpause function added in the prefab inspector. I’m not sure what else I’m missing here.

    #13206
    Terence
    Keymaster
    Helpful?
    Up
    0
    ::

    Hi Courtmaster, one thing we can check is to see if you have inadvertently set the gameIsPaused variable somewhere and forgot about it. If you’re on Windows, you can Shift + Right-click while in your project folder and use the following command:

    findstr /s /c:"gameIsPaused" *.cs

    For macs, you can use:

    grep "gameIsPaused . -r

    Let me know what your results are. Hope this is useful!

    #13210
    Courtmaster
    Participant
    Helpful?
    Up
    0
    ::

    This is what it pulled up. It doesn’t look like there’s anything extra here.

    PS C:\Users\rothc\SK Vania> findstr /s /c:"gameIsPaused" *.cs
    Assets\GameManager.cs:    public bool gameIsPaused;
    Assets\GameManager.cs:        if(Input.GetKeyDown(KeyCode.Escape) && !gameIsPaused)
    Assets\GameManager.cs:            gameIsPaused = true;
    Assets\GameManager.cs:        gameIsPaused = false;
    Assets\Scripts\Enemy.cs:        if (GameManager.Instance.gameIsPaused) return;
    Assets\Scripts\PlayerController.cs:        if (GameManager.Instance.gameIsPaused) return;

    I even added a debug log to the UnPauseGame method, and that’s working. But for some reason, the gameIsPaused bool refuses to update

        if(Input.GetKeyDown(KeyCode.Escape) && !gameIsPaused)
        {
            pauseMenu.FadeUIIn(fadeTime);
            Time.timeScale = 0;
            gameIsPaused = true;
        }
    
    
    public void UnPauseGame()
    {
        gameIsPaused = false;
        Time.timeScale = 1;
        Debug.Log("It's working");
        
    
    }

    The debug log appears in the console, and the Time.timescale returns to 1 as it should. I’ve tried moving gameIsPaused under and over the timescale, but that didn’t help either.

    #13211
    Terence
    Keymaster
    Helpful?
    Up
    0
    ::

    It seems like only the GameManager class changes the gameIsPaused variable.

    Another thing you can try is pausing the game, and typing t:GameManager into the Hierarchy, to see if you have multiple GameManager components attached to objects on your scene.

    #13212
    Courtmaster
    Participant
    Helpful?
    Up
    0
    ::

    Nope, just the one. Another thing that’s curious is that when I call UnPauseGame() in update, it seems to keep the game unpaused, so it does adjust the boolean properly, just not when I use it as a button. But everything else in the function -but that- works.

    #13213
    Courtmaster
    Participant
    Helpful?
    Up
    0
    ::

    Well, for now I just removed the gameIsPaused bool. It still sets the timescale to 0 without it, which more or less does the job. It just starts the player’s next attack when you click, then freezes. It’s a little wonky, but better than nothing.

    #13214
    Terence
    Keymaster
    Helpful?
    Up
    0
    ::

    Can you share your entire GameManager script? Let me have a look and see if I can spot anything out of the ordinary.

    #13215
    Courtmaster
    Participant
    Helpful?
    Up
    0
    ::

    There aren’t any references to the shade or half-mana as I chose not to include those features in my game. Also Bench is renamed Altar, but neither of those things should affect the pause function.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.SceneManagement;
    
    public class GameManager : MonoBehaviour
    {
        public string transitionedFromScene;
    
        public Vector2 platformingRespawnPoint;
        public Vector2 respawnPoint;
        [SerializeField] Vector2 defaultRespawnPoint;
        [SerializeField] Altar altar;
    
        [SerializeField] private FadeUI pauseMenu;
        [SerializeField] private float fadeTime;
        public bool gameIsPaused;
        
    
        public static GameManager Instance { get; private set; }
        private void Awake()
        {
            SaveData.Instance.Initialize();
            if(Instance != null && Instance != this)
            {
                Destroy(gameObject);
            }
            else
            {
                Instance = this;
            }
            SaveScene();
    
            DontDestroyOnLoad(gameObject);
            altar = FindObjectOfType<Altar>();
        }
    
        private void Update()
        {
            if(Input.GetKeyDown(KeyCode.P))
            {
                SaveData.Instance.SavePlayerData();
            }
    
            if(Input.GetKeyDown(KeyCode.Escape) && !gameIsPaused)
            {
                pauseMenu.FadeUIIn(fadeTime);
                Time.timeScale = 0;
                gameIsPaused = true;
                
                
            }
    
            
        }
    
        public void UnPauseGame()
        {
            gameIsPaused = false;
            Time.timeScale = 1;
            Debug.Log("It's working");
    
    
        }
    
        
    
        public void SaveScene()
        {
            string currentSceneName = SceneManager.GetActiveScene().name;
            SaveData.Instance.sceneNames.Add(currentSceneName);
        }
    
        public void RespawnPlayer()
        {
            SaveData.Instance.LoadAltar();
            if(SaveData.Instance.altarSceneName != null)
            {
                SceneManager.LoadScene(SaveData.Instance.altarSceneName);
            }
            
            if(SaveData.Instance.altarPos != null)
            {
                respawnPoint = SaveData.Instance.altarPos;
            }
           else
            {
                respawnPoint = defaultRespawnPoint;
            }
    
            PlayerController.Instance.transform.position = respawnPoint;
            StartCoroutine(UIManager.Instance.DeactivateDeathScreen());
            PlayerController.Instance.Respawned();
        }
        
    
    }
    #13216
    Courtmaster
    Participant
    Helpful?
    Up
    0
    ::

    If this is any help:

    If I add another bool to GameManager that is true when I pause and false when I unpause, it has the same issue where it remains true. A float variable that increases by one when I both pause and unpause will only increase when I pause, but not when I unpause.

    HOWEVER

    if I make a public bool in PlayerController, then make it true when I pause and false when I unpause, then that works perfectly. So the issue lies specifically within the unpause button updating variables within the GameManager script

    #13217
    Courtmaster
    Participant
    Helpful?
    Up
    0
    ::

    And with that in mind, I just transferred the gameIsPaused bool to the PlayerController script and now it works perfectly. Just kind of a weird bug I guess

    #13218
    Terence
    Keymaster
    Helpful?
    Up
    0
    ::

    I’ve scanned this script up and down for a couple of minutes and I couldn’t spot any issue. Strange.

    You can try adding GameManager.Instance to the gameIsPaused to be more specific about what we are manipulating and see if it works out.
     

    
        private void Update()
        {
            if(Input.GetKeyDown(KeyCode.P))
            {
                SaveData.Instance.SavePlayerData();
            }
    
            if(Input.GetKeyDown(KeyCode.Escape) && !gameIsPaused)
            {
                pauseMenu.FadeUIIn(fadeTime);
                Time.timeScale = 0;
                GameManager.Instance.gameIsPaused = true;
            }
    
            
        }
    
        public void UnPauseGame()
        {
            GameManager.Instance.gameIsPaused = false;
            Time.timeScale = 1;
            Debug.Log("It's working");
        }
    
    #13219
    Courtmaster
    Participant
    Helpful?
    Up
    0
    ::

    lmao that actually works. Bizarre. Anyway, thanks for all your help, man, you’re awesome!

    #13220
    Terence
    Keymaster
    Helpful?
    Up
    0
    ::

    No problem Courtmaster. If my fix worked, it means that there is more than 1 GameManager floating around in your Scene. That’s why gameIsPaused and GameManager.Instance.gameIsPaused give us different results.

    If you happen to find the issue, can I trouble you to post it here? Because you having more than 1 GameManager means that somewhere, our code in Awake() to delete extra GameManagers isn’t working 100%.

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

Go to Login Page →


Advertisement below: