1
\$\begingroup\$

I am using the following code which works, But the first time i press escape nothing happens, if i press it again then the game object shows. Does anyone know why this is?

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

public class PauseScript : MonoBehaviour
{
    public GameObject PauseMenu;
    bool Paused = false;

    // Start is called before the first frame update
    void Start()
    {
        PauseMenu.gameObject.SetActive(false);

    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown("escape"))
        {
            if (Paused == true)
            {
                Time.timeScale = 1.0f;
                PauseMenu.gameObject.SetActive(true);
                Paused = false;
            }
            else
            {
                Time.timeScale = 0.0f;
                PauseMenu.gameObject.SetActive(false);
                Paused = true;
            }
        }
    }
}
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

When you start the game, you are deactivating your PauseMenu and initializing your Paused bool to false.

The first time you press the escape key, you are following the else path of your if statement and deactivating the PauseMenu again, so nothing will appear.

If I understand correctly what you are trying to do, you simply need to swap your SetActive(true) and SetActive(false) calls in Update().

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .