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 5 posts - 16 through 20 (of 20 total)
  • Author
    Posts
  • #14006
    Joseph Tang
    Level 13
    Moderator
    Helpful?
    Up
    0
    ::

    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 the timeScale back to [1]. From there, we’ll create an if statement under Update() to check if the timeScale is at [1]/normal speed, and if the bool gameIsPaused is still true. Then in the If statement, set gameIsPaused 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 both Update() and UnpauseGame() instead of only UnpauseGame().

    #14017
    Anuk Thotawatta
    Level 5
    Participant
    Helpful?
    Up
    0
    ::

    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

    #14018
    Anuk Thotawatta
    Level 5
    Participant
    Helpful?
    Up
    0
    ::

    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

    #14019
    Joseph Tang
    Level 13
    Moderator
    Helpful?
    Up
    0
    ::

    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.

    #14020
    Anuk Thotawatta
    Level 5
    Participant
    Helpful?
    Up
    0
    ::

    ah it workd. Thanks a lot

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

Go to Login Page →


Advertisement below: