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

Viewing 15 posts - 1 through 15 (of 20 total)
  • Author
    Posts
  • #13991
    Anuk Thotawatta
    Participant

    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

    #13992
    Joseph Tang
    Moderator

    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 which pState bools are returning your inputs.

    #13993
    Anuk Thotawatta
    Participant

    the input loss bug appears to be in the game manager script. when i resume the gameIsPaused bool still remains true

    View post on imgur.com

    my game manager script’s Update function looks like this:

        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;
        }
    #13994
    Joseph Tang
    Moderator

    Have you checked if your “Resume” button has been assigned an on click event to call UnPauseGame()?

    #13995
    #13996
    Joseph Tang
    Moderator

    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 before Time.timeScale = 1; instead of after. If the game is still frozen, something is up with that one code.

    #13997
    Anuk Thotawatta
    Participant

    I did this to my void Update. now when i press space bar the game gets unpaused as intended

    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; 
            }
        }

    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

    public void UnpauseGame(){
            Debug.Log("started unpause");
            gameIsPaused = false;
            Debug.Log("Changed bool");
            Time.timeScale = 1;
            Debug.Log("unpause complete");
        }
    #13998
    Joseph Tang
    Moderator

    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;
        }
    #13999
    Anuk Thotawatta
    Participant

    didnt change much.

    View post on imgur.com

    #14000
    Joseph Tang
    Moderator

    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.

    #14001
    Anuk Thotawatta
    Participant

    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

    #14002
    Joseph Tang
    Moderator

    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.

    #14003
    #14004
    Joseph Tang
    Moderator

    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.

    #14005
    Anuk Thotawatta
    Participant

    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

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

Go to Login Page →


Advertisement below: