Forum begins after the advertisement:


[part 11] UI buttons dont work

Home Forums Video Game Tutorial Series Creating a Metroidvania in Unity [part 11] UI buttons dont work

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #16708
    Niyam Shah
    Level 8
    Participant
    Helpful?
    Up
    0
    ::

    so i just finished part 11 and everything works perfecty fine except now the UI buttons on the pause menu dont work. if i click “resume”, the pause menu will dissapear but the scene will be frozen still. and if i click quit to main menu, nothing happens.

    this isnt a code bug i think since the game pauses and unpauses if i click the “escape” key. ive checked a few times and both buttons seemed to be configured correctly?

    View post on imgur.com
        private void Update()
        {
            if (Input.GetKeyDown(KeyCode.P))
            {
                SaveData.Instance.SavePlayerData();
            }
    
            if (Input.GetKeyDown(KeyCode.Escape))
            {
                Pause(!isPaused);
            }
        }
    
        public void Pause(bool b)
        {
            if (b)
            {
    
                if (lastTimeScale < 0)
    
                    lastTimeScale = Time.timeScale;
                Time.timeScale = 0;
            }
            else
            {
                if (!isStopped)
                {
                    Time.timeScale = lastTimeScale;
                    lastTimeScale = -1;
                }
            }
    
            pauseMenu.Fade(fadeTime, b);
            isPaused = b;
    
        }
    
    
        public static void Stop(float duration = 0.5f, float restoreDelay = 0.1f, float slowMultiplier = 0f)
        {
            if (stopGameCoroutine != null) return;
            stopGameCoroutine = Instance.StartCoroutine(HandleStopGame(duration, restoreDelay, slowMultiplier));
        }
    
    
        static IEnumerator HandleStopGame(float duration, float restoreDelay, float slowMultiplier = 0f)
        {
            if(Instance.lastTimeScale < 0)
                Instance.lastTimeScale = Time.timeScale;
            Time.timeScale = Instance.lastTimeScale  * slowMultiplier;
    
            WaitForEndOfFrame w = new WaitForEndOfFrame();
    
            while(duration > 0)
            {
                if (Instance.isPaused)
                {
                    yield return w;
    
                    continue;
                }
    
                Time.timeScale = Instance.lastTimeScale * slowMultiplier;
    
                duration -= Time.unscaledDeltaTime;
                yield return w;
            }
    
            float timeScaleToRestore = Instance.lastTimeScale;
    
            Instance.lastTimeScale = -1;
            stopGameCoroutine = null;
    
            if(restoreDelay > 0)
            {
                float currentTimeScale = timeScaleToRestore * slowMultiplier;
                float restoreSpeed = (timeScaleToRestore - currentTimeScale) / restoreDelay;
                while(currentTimeScale < timeScaleToRestore)
                {
                    if (Instance.isPaused)
                    {
                        yield return w;
                        continue;
                    }
    
                    if (isStopped) yield break;
    
                    currentTimeScale += restoreSpeed * Time.unscaledDeltaTime;
                    Time.timeScale = currentTimeScale;
    
                    yield return w;
                }
            }
    
            if (!isStopped) Time.timeScale = timeScaleToRestore;
        }
    #16711
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    Make sure that the Canvas Group on the Pause Screen has both Interactable and Blocks Raycasts checked. If you added a UI Screen without adding a Canvas Group, the Canvas Group that is automatically added will not have both checked.

    #16726
    Niyam Shah
    Level 8
    Participant
    Helpful?
    Up
    0
    ::

    yep so i checked both but the error still persists

    #16741
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    Is another UI element blocking your Pause buttons? Try deleting everything in your Canvas temporarily, then running the game to see if you can click on the buttons on the Pause screen.

    You may find this short helpful as well:

    #16748
    Niyam Shah
    Level 8
    Participant
    Helpful?
    Up
    0
    ::

    Oh no. The buttons are interactiable since when I click on them, the pause screen will disappear but the functions of sending me to the main menu or unpausing the game won’t work until I click the escape key after unpausing. I think the functions are being affected by the time stop?

    #16757
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    Double check that the actions are assigned properly, as changing the functions in GameManager sometimes causes the actions to be removed:

    Resume Quit to Main Menu

    And make sure the unpause action is positioned first, so that the time scale is set back to 1.

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

Go to Login Page →


Advertisement below: