kajvans
kajvans

Reputation: 85

to menu from the pausescreen C#

in this code i did make a pause menu with some buttons like resume and Menu. if i click menu i go to the menu but the time is still frozen i can play the game further if i press escape again. why does it do that. if you press menu i goes to the menu and it activates the Resume method. the resume method works fine if i just press resume but when i click menu the method is not working.

using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.SceneManagement;
    
    public class PauseMenu : MonoBehaviour
    {
    
        public static bool GameIsPaused = false;
    
        public GameObject pauseMenuUI;
    
        void Update()
        {
            if (Input.GetKeyDown(KeyCode.Escape))
            {
                if (GameIsPaused)
                {
                    Resume();
                }
                else
                {
                    Pause();
                }
            }
        }
    
        public void Menu()
        {
            Resume();
            SceneManager.LoadScene(0);
        }
    
        public void Resume()
        {
            pauseMenuUI.SetActive(false);
            Time.timeScale = 1f;
            GameIsPaused = false;
        }
    
        public void Pause()
        {
            pauseMenuUI.SetActive(true);
            Time.timeScale = 0f;
            GameIsPaused = true;
        }
    }

I have trier to server timescale to normal when i click menu but still the timescale is 0 so you have to go again in the pause menu to click resume and than the timescale is correct again but that is not how it is supposed to work.

Upvotes: 1

Views: 40

Answers (1)

OnePrograms
OnePrograms

Reputation: 26

The code should work, but did you set your script to the pause menu canvas? If so, you also need to set an OnClick() Event on each button in the inspector.

Hope that will work!

Upvotes: 1

Related Questions