Forum begins after the advertisement:
[part 9] Unpausing malfunction
Home › Forums › Video Game Tutorial Series › Creating a Metroidvania in Unity › [part 9] Unpausing malfunction
- This topic has 12 replies, 2 voices, and was last updated 10 months, 3 weeks ago by Terence.
-
AuthorPosts
-
February 1, 2024 at 1:25 am #13205::
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.
February 1, 2024 at 11:41 am #13206::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!
February 2, 2024 at 11:06 pm #13210::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.
February 2, 2024 at 11:26 pm #13211::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.February 2, 2024 at 11:37 pm #13212::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.
February 2, 2024 at 11:46 pm #13213::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.
February 3, 2024 at 12:00 am #13214::Can you share your entire GameManager script? Let me have a look and see if I can spot anything out of the ordinary.
February 3, 2024 at 12:08 am #13215::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(); } }
February 3, 2024 at 12:28 am #13216::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
February 3, 2024 at 12:38 am #13217::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
February 3, 2024 at 12:41 am #13218::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 thegameIsPaused
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"); }
February 3, 2024 at 1:15 am #13219::lmao that actually works. Bizarre. Anyway, thanks for all your help, man, you’re awesome!
February 3, 2024 at 1:55 pm #13220::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
andGameManager.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%. -
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: