Forum begins after the advertisement:
[part 9] pausing and resuming causes loss of control
Home › Forums › Video Game Tutorial Series › Creating a Metroidvania in Unity › [part 9] pausing and resuming causes loss of control
- This topic has 19 replies, 2 voices, and was last updated 9 months ago by Anuk Thotawatta.
-
AuthorPosts
-
April 20, 2024 at 3:44 am #14006::
Okay, new idea. I don’t think your button can set bools for some reason. So let’s try this:
public bool Resumed = false;private void Update() { if(Input.GetKeyDown(KeyCode.P)){ SaveData.Instance.SavePlayerData(); Debug.Log("Saving Data..."); } if(Input.GetKeyDown(KeyCode.Escape) && !gameIsPaused){ pauseMenu.FadeUIIn(fadeTime); Time.timeScale = 0; gameIsPaused = true; }if(Resume)if(Time.timeScale = 1 && gameIsPaused) { gameIsPaused = false;Time.timeScale = 1; Resumed = false;} } public void UnpauseGame() { Time.timeScale = 1;Resumed = true;}Now your code should work. Your resume button only seems to be able to set the timescale, for some reason, and the update function works in setting everything else. Thus, when your
UnpauseGame()
method is called, we’ll set it to only set thetimeScale
back to [1]. From there, we’ll create an if statement underUpdate()
to check if thetimeScale
is at [1]/normal speed, and if the boolgameIsPaused
is still true. Then in the If statement, setgameIsPaused
to false.By the way, it seems like manually pressing Resumed does work, looking at the fact it turns off
gameIsPaused
. It’s just that you’ve commented out the code for setting the timescale to normal in bothUpdate()
andUnpauseGame()
instead of onlyUnpauseGame()
.April 20, 2024 at 1:54 pm #14017::is there to way to fix that bug in the game manager that gives null reference exception error when i quit in pause menu and restart via main menu
April 20, 2024 at 2:04 pm #14018::Holy sh*t it finally works. thank you so much. i changed your above code to
if(Time.timeScale == 1 && gameIsPaused)
as it gave me an error but now the pause button works perfectly.View post on imgur.com
now i only have to find a way to keep game manager script from losing the fadeUI reference when going back to main menu and re-entering game
April 20, 2024 at 2:06 pm #14019::As mentioned before “As for the reason why the gamemanager has the nullreference, it’s because the Canvas prefab has the script to destroy in main menu, while the gamemanager has dontdestroyonload. Thus, the gamemanager can go to the main menu, while the canvas cant, and the gamemanager loses the scenefade object because of it. There is also no code to be able to allow the gamemanager to reset that fact.
I’m unsure if you need the gamemanager in the main menu, so you can put a destroyinmainmenu.cs on the gamemanager too.”
TLDR: try adding destroyinmainmenu to your gamemanager.
April 20, 2024 at 2:22 pm #14020 -
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: