Problem with showing GUI and with stopping game

When I’m pressing Escape, my menu is showing and music is playing. Its good but I need to have stoped game and workable menu then. And when I have my menu acrived, and I am pressing W or S or A or D, the player is running as my buttons in menu :frowning:
Another problem is that when I am pressing Load button or Save button, I would like to run Load or Save script AND create text:“Saved” or “Loaded” in right bottom corner for maybe 2 sec. Yield isnt work :frowning: Can somebody help me? :smiley: :slight_smile: Thanks! :stuck_out_tongue:

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System.Collections;
using System.Collections.Generic;

public class PauseGame : MonoBehaviour {

    public GameObject pauseMenu;
    public GameObject savePanel;
    public GameObject loadPanel;
    private bool isEnabled = false;

    void Start()
    {
        pauseMenu.SetActive(false);
        savePanel.SetActive(false);
        loadPanel.SetActive(false);
    }

    void Update()
    {
        // Enable pause menu
        if ((Input.GetKeyDown(KeyCode.Escape)) && !isEnabled)
        {
            pauseMenu.SetActive(true);
            Cursor.visible = true;
            isEnabled = true;
        }

        // disable pause menu
        else if ((Input.GetKeyDown(KeyCode.Escape)) && isEnabled)
        {
            pauseMenu.SetActive(false);
            Cursor.visible = false;
            isEnabled = false;
        }

        //if (isEnabled)
        //{
        //    Time.timeScale = 0;
        //}else Time.timeScale = 1;
    }
       
    public void Resume()
    {
        isEnabled = false;
    }

    public void MainMenu()
    {
        SceneManager.LoadScene (0);
    }

    IEnumerator Save()
    {
        PlayerPrefs.SetInt("currentscenesave", SceneManager.GetActiveScene().buildIndex);
        savePanel.SetActive(true);
        yield return new WaitForSeconds(1);
        savePanel.SetActive(false);
        isEnabled = false;
    }

    IEnumerator Load()
    {
        SceneManager.LoadScene(PlayerPrefs.GetInt("currentscenesave"));
        loadPanel.SetActive(true);
        yield return new WaitForSeconds(1);
        loadPanel.SetActive(false);
        isEnabled = false;
    }

}

WaitForSeconds is affected by timescale, and with timescale 0 it will never complete. Use WaitForSecondsRealtime instead.

To keep your char from moving, create a bool. Could be named isPaused. When true, don’t allow movement. Set this bool when you open or close the menu. (hitting esc)

Yeap. But when I done this, I have got error when I press Load Button or Save Button (“ArgumentException: method return type is incompatible”):

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System.Collections;
using System.Collections.Generic;

public class PauseGame : MonoBehaviour {

    public GameObject pauseMenu;
    public GameObject savePanel;
    public GameObject loadPanel;
    private bool isEnabled = false;
    private bool isPaused = false;

    void Start()
    {
        pauseMenu.SetActive(false);
        savePanel.SetActive(false);
        loadPanel.SetActive(false);
        Cursor.visible = false;
    }

    void Update()
    {
        // Enable pause menu
        if ((Input.GetKeyDown(KeyCode.Escape)) && !isEnabled)
        {
            pauseMenu.SetActive(true);
            Cursor.visible = true;
            isEnabled = true;
        }

        // disable pause menu
        else if ((Input.GetKeyDown(KeyCode.Escape)) && isEnabled)
        {
            pauseMenu.SetActive(false);
            Cursor.visible = false;
            isEnabled = false;
        }

        //if (isEnabled)
        //{
        //    Time.timeScale = 0;
        //}else Time.timeScale = 1;
    }
       
    public void Resume()
    {
        isEnabled = false;
    }

    public void MainMenu()
    {
        SceneManager.LoadScene (0);
    }

    IEnumerator Save()
    {
        PlayerPrefs.SetInt("currentscenesave", SceneManager.GetActiveScene().buildIndex);
        savePanel.SetActive(true);
        yield return new WaitForSecondsRealtime(5);
        savePanel.SetActive(false);
        isEnabled = false;
    }

    IEnumerator Load()
    {
        SceneManager.LoadScene(PlayerPrefs.GetInt("currentscenesave"));
        loadPanel.SetActive(true);
        yield return new WaitForSecondsRealtime(5);
        loadPanel.SetActive(false);
        isEnabled = false;
    }

}

But how to do this? I have somethink like this:

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System.Collections;
using System.Collections.Generic;

public class PauseGame : MonoBehaviour {

    public GameObject pauseMenu;
    public GameObject savePanel;
    public GameObject loadPanel;
    private bool isEnabled = false;
    private bool isPaused = false;

    void Start()
    {
        pauseMenu.SetActive(false);
        savePanel.SetActive(false);
        loadPanel.SetActive(false);
        Cursor.visible = false;
    }

    void Update()
    {
        // Enable pause menu
        if ((Input.GetKeyDown(KeyCode.Escape)) && !isEnabled)
        {
            pauseMenu.SetActive(true);
            Cursor.visible = true;
            isEnabled = true;
        }

        // disable pause menu
        else if ((Input.GetKeyDown(KeyCode.Escape)) && isEnabled)
        {
            pauseMenu.SetActive(false);
            Cursor.visible = false;
            isEnabled = false;
        }

        //if (isEnabled)
        //{
        //    Time.timeScale = 0;
        //}else Time.timeScale = 1;
    }
       
    public void Resume()
    {
        isEnabled = false;
    }

    public void MainMenu()
    {
        SceneManager.LoadScene (0);
    }

    IEnumerator Save()
    {
        PlayerPrefs.SetInt("currentscenesave", SceneManager.GetActiveScene().buildIndex);
        savePanel.SetActive(true);
        yield return new WaitForSecondsRealtime(5);
        savePanel.SetActive(false);
        isEnabled = false;
    }

    IEnumerator Load()
    {
        SceneManager.LoadScene(PlayerPrefs.GetInt("currentscenesave"));
        loadPanel.SetActive(true);
        yield return new WaitForSecondsRealtime(5);
        loadPanel.SetActive(false);
        isEnabled = false;
    }

}

Chances are you’ll want to make isPaused public static. Then in your update when you check for escape, when the dialog box comes up, you’ll make isPaused true. When it’s closed, you’ll make isPaused false.

Then, on your player char you can simply do a check for
if(!PauseGame.isPaused) //checks if game is not paused
{
//Handle normal movement stuff.
}

Note that if you have a gameManager script, you can put the static isPaused there or pretty much anywhere, but usually for organization, somewhere like that is a good spot to put it.

When I done this too in player animation script its working but my char just stuck in the down running animation and loop this all the time :stuck_out_tongue:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerAnimation : MonoBehaviour {

    public Animator anim;

    void Start ()
    {
        anim = GetComponent<Animator> ();
    }

    void Update ()
    {
        if (!PauseGame.isPaused) {
            if ((Input.GetKey (KeyCode.W)) || ((Input.GetKey (KeyCode.UpArrow)))) {
                anim.SetBool ("Up", true);
                anim.SetBool ("Left", false);
                anim.SetBool ("Down", false);
                anim.SetBool ("Right", false);
            }


            if ((Input.GetKey (KeyCode.S)) || ((Input.GetKey (KeyCode.DownArrow)))) {
                anim.SetBool ("Down", true);
                anim.SetBool ("Left", false);
                anim.SetBool ("Up", false);
                anim.SetBool ("Right", false);
            }

            if ((Input.GetKey (KeyCode.A)) || ((Input.GetKey (KeyCode.LeftArrow)))) {
                anim.SetBool ("Left", true);
                anim.SetBool ("Down", false);
                anim.SetBool ("Up", false);
                anim.SetBool ("Right", false);
            }
            if ((Input.GetKey (KeyCode.D)) || ((Input.GetKey (KeyCode.RightArrow)))) {
                anim.SetBool ("Right", true);
                anim.SetBool ("Left", false);
                anim.SetBool ("Down", false);
                anim.SetBool ("Up", false);
            }


            if ((Input.GetKey (KeyCode.W)) || (Input.GetKey (KeyCode.UpArrow))) {
                anim.SetBool ("WalkUp", true);
            } else {
                anim.SetBool ("WalkUp", false);
            }

            if ((Input.GetKey (KeyCode.S)) || (Input.GetKey (KeyCode.DownArrow))) {
                anim.SetBool ("WalkDown", true);
            } else {
                anim.SetBool ("WalkDown", false);
            }

            if ((Input.GetKey (KeyCode.A)) || (Input.GetKey (KeyCode.LeftArrow))) {
                anim.SetBool ("WalkLeft", true);
            } else {
                anim.SetBool ("WalkLeft", false);
            }

            if ((Input.GetKey (KeyCode.D)) || (Input.GetKey (KeyCode.RightArrow))) {
                anim.SetBool ("WalkRight", true);
            } else {
                anim.SetBool ("WalkRight", false);
            }
        }
           
    }

}