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 8 months ago by Anuk Thotawatta.
-
AuthorPosts
-
April 19, 2024 at 5:59 pm #13991::
first of all when I start the game from the main menu, it pauses the game giving an null reference exception error. I unpaused via unity and run the game, paused it by pressing Esc and resumed it, then the gmae didnt take any of my inputs. same happened when I quit the game by the pause menu and returned to the game by the main menu.
View post on imgur.com
April 19, 2024 at 6:52 pm #13992::I can’t see it clearly, what error is showing up on the console?
Can you check what error is returned from the main menu, where it goes to, and what code is it? Perhaps an unassigned variable in the inspector.
As for your Player inputs, please check the PlayerStates.cs on your Player while in the error for which bools are checked, then check your
Update()
function for whichpState
bools are returning your inputs.April 19, 2024 at 10:05 pm #13993::the input loss bug appears to be in the game manager script. when i resume the
gameIsPaused
bool still remains trueView post on imgur.com
my game manager script’s Update function looks like this:
<code> 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; } } public void UnPauseGame(){ Time.timeScale = 1; gameIsPaused = false; }</code>
April 19, 2024 at 11:20 pm #13994::Have you checked if your “Resume” button has been assigned an on click event to call
UnPauseGame()
?April 20, 2024 at 12:34 am #13995April 20, 2024 at 1:03 am #13996::Try putting a print code before and after the
gameIsPaused = false;
to check if the code is firing or not. The only reason for the issue is that the bool is not changed to false and thus you cannot move, the time change is working as per normal.I don’t believe it’ll change much, but try putting
gameIsPaused = false;
to beforeTime.timeScale = 1;
instead of after. If the game is still frozen, something is up with that one code.April 20, 2024 at 1:42 am #13997::I did this to my void Update. now when i press space bar the game gets unpaused as intended
<code>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(Input.GetKeyDown(KeyCode.Space) && gameIsPaused){ pauseMenu.FadeUIOut(fadeTime); Time.timeScale = 1; gameIsPaused = false; } }</code>
I also tested by adding debug lines and changing the ispaused and timescale lines, but the debug lines all printed out but
gameIsPaused
is still active after unpausing. its like the script is ignoring that one line<code>public void UnpauseGame(){ Debug.Log("started unpause"); gameIsPaused = false; Debug.Log("Changed bool"); Time.timeScale = 1; Debug.Log("unpause complete"); }</code>
April 20, 2024 at 2:21 am #13998::If the debug logs are coming back okay, then the code works fine. Perhaps something is turning the bool gameispaused back on? Maybe your
gameIsPaused
bool is not set to public?If you can’t find any other code that’s turning the bool on, one extra solution (that i would say is fairly unsustainable):
Add a new bool variable to check that you’ve pressed the resume button, then like what you did with the Spacebar key, set the if statement to check for the bool.
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(Resumed) { Time.timeScale = 1; gameIsPaused = false; Resumed = false; } } public void UnpauseGame() { Resumed = true; }
April 20, 2024 at 2:30 am #13999April 20, 2024 at 2:35 am #14000::Let’s try this:
if(Resumed) { Time.timeScale = 1; gameIsPaused = false; if(!gameIsPaused) { Resumed = false; } }
the code should be repeating until the gameispaused is finally turned false.
April 20, 2024 at 2:51 am #14001::View post on imgur.com
didnt change a thing.
another problem i noticed. is once i pause and then quit to main menu and restart game, and try to pause again the game crashes with a null reference exception error pointing to the
pauseMenu.FadeUIIn(fadeTime);
line in game manager.View post on imgur.com
Im sorry if im bothering you
April 20, 2024 at 3:01 am #14002::In the Unpausegame method, could you remove the timescale line? I would like to see whether the unpausegame method sets any bool on…
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.
April 20, 2024 at 3:14 am #14003April 20, 2024 at 3:18 am #14004::Could you try it using the same Resume bool idea? Without setting the time scale in the unpausegame method but only in the if statement update. You can even put it in the if statement for turning the resume bool off.
If it doesn’t work, while in play, try manually turning the resume bool on to see if the bool itself and the update functions made works.
April 20, 2024 at 3:35 am #14005::manually pressing resume doesnt do anything. and removing timscale makes the game freeze when Unpausing
View post on imgur.com
View post on imgur.com
for now ill just keep gameIsPaused always false so the player still cant move
-
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: