Search Unity

IEnumerator pause menu?

Discussion in 'Scripting' started by Quist, Nov 22, 2017.

  1. Quist

    Quist

    Joined:
    Feb 25, 2014
    Posts:
    284
    Hey Guys!
    I have recently switched from JS to C# which I just found out required me to go from Update() to IEnumerator when making my Pause Menu script..

    So, can someone help me correct my current broken script, so that the game sets Menu active, while freezing the game on first press of the escape key, while doing the opposite on the second push?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class Pause : MonoBehaviour
    7. {
    8.  
    9.     public GameObject Menu;
    10.     bool Freeze = false;
    11.  
    12.     void Start ()
    13.     {
    14.      
    15.     }
    16.  
    17.  
    18. private IEnumerator Pausemenu()
    19.     {
    20.         while (Input.GetKey(KeyCode.Escape) && Freeze == false)
    21.         {
    22.             Menu.SetActive(true);
    23.             Time.timeScale = 0.0f;
    24.             yield return new WaitForSeconds(1);
    25.             Freeze = true;
    26.         }
    27.        while (Input.GetKey(KeyCode.Escape) && Freeze == true)
    28.         {
    29.             Menu.SetActive(false);
    30.             Time.timeScale = 1;
    31.             yield return new WaitForSeconds(1);
    32.             Freeze = false;
    33.         }
    34.  
    35.     }
    36. }
    37.  
     
  2. jeffreyrampineda

    jeffreyrampineda

    Joined:
    Mar 26, 2014
    Posts:
    7
    You don't need IEnumerator. Try this by danivdwerf https://answers.unity.com/questions/1230216/a-proper-way-to-pause-a-game.html :

    Code (CSharp):
    1. void Update(){
    2.     if(Input.GetKeyDown(KeyCode.Escape)){
    3.         if(Menu.activeInHierarchy){
    4.             Time.timeScale = 0;
    5.             Menu.SetActive(false);
    6.         } else {
    7.             Time.timeScale = 1;
    8.             Menu.SetActive(true);
    9.         }
    10.     }
    11. }
    12.  
     
    Quist likes this.
  3. Quist

    Quist

    Joined:
    Feb 25, 2014
    Posts:
    284
    Alright, so I tried this but when "escape" is pressed the menu wil open and close a million times / second.

    So I have tried using a coroutine, but since Update is not called during Time.timeScale = 0, I can't close the menu again, any help to get?

    My new code
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Pause2 : MonoBehaviour
    6. {
    7.  
    8.     public GameObject Menu;
    9.     bool Freeze = false;
    10.  
    11.     void Start()
    12.     {
    13.  
    14.     }
    15.  
    16.  
    17.     void Update()
    18.     {
    19.         if (Input.GetKey(KeyCode.Escape) && Time.timeScale == 1)
    20.         {
    21.             StartCoroutine(Paused());
    22.         }
    23.  
    24.         if (Input.GetKey(KeyCode.Escape) && Time.timeScale == 0)
    25.         {
    26.             StartCoroutine(unPaused());
    27.         }
    28.     }
    29.  
    30.     private IEnumerator Paused()
    31.     {
    32.         Time.timeScale = 0;
    33.         Menu.SetActive(true);
    34.         yield return new WaitForSeconds(1.0f);
    35.     }
    36.     private IEnumerator unPaused()
    37.     {
    38.         yield return new WaitForSeconds(0.5f);
    39.         Time.timeScale = 1;
    40.         Menu.SetActive(false);
    41.     }
    42. }
     
  4. jeffreyrampineda

    jeffreyrampineda

    Joined:
    Mar 26, 2014
    Posts:
    7
    Sorry for late reply! Try using Input.GeyKeyDown() instead of Input.GetKey() because Input.GetKey() returns true when you hold it. Input.GetKeyDown() returns true once. Try doing that without using IEnumerator.

    Code (CSharp):
    1.     void Update()
    2.     {
    3.         if (Input.GetKeyDown(KeyCode.Escape) && Time.timeScale == 1)
    4.         {
    5.                 Time.timeScale = 0;
    6.                 Menu.SetActive(true);
    7.         }
    8.         if (Input.GetKeyDown(KeyCode.Escape) && Time.timeScale == 0)
    9.         {
    10.                 Time.timeScale = 1;
    11.                 Menu.SetActive(false);
    12.         }
    13.     }
     
    Quist likes this.
  5. Quist

    Quist

    Joined:
    Feb 25, 2014
    Posts:
    284
    Ahh yes of course! I will try it out later, but it makes perfect sense.
    Thanks a lot for helping a blind man!