Hi!
My Pausemenu script isnt working and I dont know why. The pause is really happening, so the timescale works but the menu doesnt get up, it just gets the Error: "Cannot implicitly convert type ‘UnityEngine.GameObject’ to ‘UnityEngine.GameObject’
Thats my Script:
public bool Menu;
private GameObject pauseObjects = GameObject.FindGameObjectsWithTag("Menu");
void Start ()
{
Menu = false;
}
void Update ()
{
if (Input.GetKeyUp (KeyCode.Escape)) {
Menu = !Menu;
}
if (Menu) {
Time.timeScale = 0f;
foreach (GameObject g in pauseObjects) {
g.SetActive (true);
}
} else {
Time.timeScale = 1.0f;
foreach (GameObject g in pauseObjects) {
g.SetActive (false);
}
}
}
You need to declare pauseObjects
as an array. Like this:
private GameObject[] pauseObjects;
and then set it in start
void Start()
{
pauseObjects = GameObject.FindGameObjectsWithTag("Menu");
}
EDIT: Improvement based on comments below
public bool Menu;
public GameObject menuScreen;
//Make your menu objects children of an empty gameObject on your canvas
//and drag that parent gameObject to this variable in the inspector
void Start ()
{
//bools are set to false by default so no need to declare it in start
}
void Update ()
{
//If pressing escape is the only way to pause...
//Then the menu checks can be moved to within the escape code listener
//to cut down on the number of conditions running on update
if (Input.GetKeyUp(KeyCode.Escape))
{
Menu = !Menu;
if (Menu)
{
Time.timeScale = 0f;
menuScreen.SetActive(true);
}
else
{
Time.timeScale = 1.0f;
menuScreen.SetActive(false);
}
}
}