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
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 Can somebody help me? Thanks!
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;
}
}
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)
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.