简体   繁体   中英

Unable to resume unity3D after pause

I am rather new to c#. I am currently making a pause and resume screen. Pausing seems to work just fine, but the resuming functionality isn't.

I used Debug.Log to narrow the problem down to the chunk of code below. When I hit the resume button after pausing, Paused get set to false , but nothing more happens, it's like if the code never reached the else clause.

However, it does work, as before I hit pause, I get "resume" from Debug.Log , but whenever I hit pause, I never get that.

public static bool Paused;
public void ClickPauseButton()
{
    Paused = true;
    Time.timeScale = 0;
}

public void ClickResumeButton()//works
{
    Paused = false;
    Time.timeScale = 1;
}

void Update()
{
    if(Paused == true)
    {
        showPaused();
        HideUI();
    }
    else //This is the problem... Not a single resume
    {
        ShowUI();
        hidePaused();
        Debug.Log("resume");
    }
}

You can change your script with my script which I have given below.

    using System.Collections.Generic;
    using UnityEngine;

    public class PausedMenu : MonoBehaviour
    {
        public static bool Paused = false;
        public GameObject pauseUI;

        // Update is called once per frame
        void Update()
        {
            if (Input.GetKeyDown(KeyCode.Escape))
            {
                if (Paused)
                {
                    ClickResumeButton();
                }
                else
                {
                    ClickPauseButton();
                }
            }
        }

        public void ClickResumeButton()
        {
            pauseUI.SetActive(false);
            Time.timeScale = 1f;
            Paused = false;
        }

        public void ClickPauseButton()
        {
            pauseUI.SetActive(true);
            Time.timeScale = 0f;
            Paused = true;
        }
   }

using this you can easily manage your pauseUI.

Note : Attach your script to the parent game object. Make sure your game object is in this sequence. PauseCanvas -> PauseUI -> ResumeButton. you have to give this script to PauseCanvas. And your pause button is outside of pauseUI.

You can use this sequence which I have given below.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM