- Home /
C# problem with pause menu
First of all my english is bad so sorry my mistakes writting. I'm trying to make work a pause menu which use canvas and timescale works, but if I press the movement keycodes the object still moving so i decided if the timescale = 0 disable the scrip of the object but i had a lot of issues and i can't find a solution so anyone could help me? this is my pause script: public class pause : MonoBehaviour { public Transform canvas;
 // Use this for initialization
 void Start () {
     
 }
         
 // Update is called once per frame
 void Update () 
 {
     if(Input.GetKeyDown (KeyCode.Escape)) 
     {
         if (canvas.gameObject.activeInHierarchy == false) {
             canvas.gameObject.SetActive (true);
             Time.timeScale = 0;
             if (canvas.gameObject.SetActive (true)) 
             {
                 GetComponent<Group> ().enabled =(false);
             }
         } 
     }
 }
 public void Return()
 {
     canvas.gameObject.SetActive (false);
     Time.timeScale = 1;
     if (canvas.gameObject.SetActive (false)) 
     {
         GetComponent<Group> ().enabled =(true);
     }
 }
 public void Exit()
 {
     SceneManager.LoadScene ("MainMenu");
 }
Answer by Masterio · Aug 07, 2016 at 04:06 PM
You can set the time scale to 0.00001f or make your UI scripts independends from the time scale by using custom time (you can find it on this forum).
$$anonymous$$y game is a tetris and the movement of the pieces doesn't work with a float. That's how it works: void Update () {
     if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.LeftArrow)) {
         // $$anonymous$$odify position
         transform.position += new Vector3(-1, 0, 0);
         // See if valid
         if (isValidGridPos())
             // Its valid. Update grid.
             updateGrid();
         else
             // Its not valid. revert.
             transform.position += new Vector3(1, 0, 0);
You can apply this solution if you have some energy for rework updated metohds:
 Create static variable paused in class Game (you can place this static variable in any class, this class should be permament per level i mean do not remove this object when level is loaded)
 
 public class Game : $$anonymous$$onoBehaviour
 {
    public static bool paused = false;
 }
 
Next place in every update method what execute block spawn/move this lines:
 if(Game.paused)
    return;
 
Next if you pause the game set the Game.paused = true; if you unpause the game then Game.paused = false;
this script can be optimized by the:
  if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.LeftArrow)) 
     {
       if ( transform.position.x > leftBorderX)
       {
             // move if position is valid
              transform.position -= new Vector3(1, 0, 0);
     
               updateGrid();
       }
     else if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.RightArrow)) 
     {
 
       if ( transform.position.x < rightBorderX)
       {
             // move if position is valid
              transform.position += new Vector3(1, 0, 0);
     
               updateGrid();
       }
 
 }
 
 
Answer by danivdwerf · Aug 08, 2016 at 07:27 AM
Try this maybe
Don't know if how you did it in your hiearchy, but you should put all your pause stuff in a panel
public class Pause : MonoBehaviour 
{
    [SerializeField] private GameObject pausePanel;
    
    void Start()
    {
        pausePanel.SetActive(false);
    }
    
    void Update()
    {
        if(Input.GetKeyDown (KeyCode.Escape)) 
        {
            if (canvas.gameObject.activeInHierarchy == false) 
            {
                PauseGame();
            }
            if (canvas.gameObject.SetActive (true)) 
            {
                 ContinueGame();   
            }
        } 
     }
    
    private void PauseGame()
    {
        Time.timeScale = 0;
        pausePanel.SetActive(true);
        /DisableControllers
    } 
    
    private void ContinueGame()
    {
        Time.timeScale = 1;
        pausePanel.SetActive(false);
        /EnableControllers
    }
}
 
               Also, did you make an empty object called GameController where you put the pause script on? it's not required, but very helpfull
Good luck
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                