- Home /
Problem with Pause Menu because of WaitForSeconds
I'm making a pause menu for a controller(xbox), also on the keyboard with the use od arrow keys and w,s. The problem is since there is no delay, it automatically goes to the last on possible, since there's 3 options, it only alternates between the first and last, so we are unable to access the second option. I've tried using WaitForSeconds and a delay to get this result but it hasn't worked. Here's my code so far.
using UnityEngine;
using System.Collections;
public class PauseMenu : MonoBehaviour { private bool paused = false; private delegate void PauseDelegate(); private PauseDelegate currentPauseDelegate; //allows variable to be created through menuFunction
private float screenHeight;
private float screenWidth;
private float buttonHeight;
private float buttonWidth;
public GUIStyle font;
private string[] menu = {"Jogar","Opcoes", "Sair",};
private int selectionIndex = 0;
private bool clicked = false;
public float keyDelay = 0.25f;
public bool canUse = true;
void Start()
{
screenHeight = Screen.height;
screenWidth = Screen.width;
buttonHeight = screenHeight * 0.07f;
buttonWidth = screenWidth * 0.18f;
this.currentPauseDelegate = pauseMenu;
}
void Update()
{
Debug.Log("item selecionado"+selectionIndex);
if (Input.GetButtonDown("Pause"))
paused = togglePause();
while (true)
{
if(Input.GetAxisRaw("Vertical") > 0.5)
{
selectionIndex--;
if(selectionIndex < 0)
{
selectionIndex = 0 ;
}
//canUse = false;
StartCoroutine(InputDelay());
//yield return new WaitForSeconds(keyDelay);
}
if(Input.GetAxisRaw("Vertical") < -0.5)
{
selectionIndex++;
if(selectionIndex >= menu.Length)
{
selectionIndex = menu.Length -1;
}
//canUse = false;
StartCoroutine(InputDelay());
//yield return new WaitForSeconds(keyDelay);
}
}
clicked = false;
if(Input.GetButtonDown("Jump") || Input.GetKeyDown("enter"))
{
clicked = true;
}
}
IEnumerator InputDelay()
{
print("BEFORE");
yield return new WaitForSeconds(1);
//yield return new WaitForSeconds(keyDelay);
print("AFTER");
canUse = true;
}
void OnGUI()
{
this.currentPauseDelegate();
}
void pauseMenu()
{
if (paused)
{
GUI.Box(new Rect(0, 0, Screen.width, Screen.height), "");
GUI.FocusControl(menu[selectionIndex]);
GUI.SetNextControlName ("Jogar");
if(GUI.Button(new Rect ((screenWidth - buttonWidth) * 0.1f, screenHeight * 0.3f, buttonWidth, buttonHeight) , new GUIContent("Jogar"), font) || clicked)
{
if(GUI.GetNameOfFocusedControl() == "Jogar")
{
//do something
Debug.Log("voce ativou o botao jogar");
paused = togglePause();
}
}
GUI.SetNextControlName ("Opcoes");
if(GUI.Button(new Rect ((screenWidth - buttonWidth) * 0.1f, screenHeight * 0.4f, buttonWidth, buttonHeight), new GUIContent("Opcoes"), font) || clicked)
{
if(GUI.GetNameOfFocusedControl() == "Opcoes")
{
//do something
//this.currentPauseDelegate = OptionsMenu;
Debug.Log("voce ativou o botao opcoes");
}
}
GUI.SetNextControlName ("Sair");
if(GUI.Button(new Rect ((screenWidth - buttonWidth) * 0.1f, screenHeight * 0.5f, buttonWidth, buttonHeight), new GUIContent("Sair"), font) || clicked)
{
if(GUI.GetNameOfFocusedControl() == "Sair")
{
Debug.Log("voce ativou o botao sair");
//do something
}
}
}
}
bool togglePause()
{
//nome do script da camera
WowCamera2 xDeg = Camera.main.GetComponent<WowCamera2>();
WowCamera2 yDeg = Camera.main.GetComponent<WowCamera2>();
if (Time.timeScale == 0f)
{
xDeg.enabled = true;
yDeg.enabled = true;
Time.timeScale = 1f;
return (false);
}
else
{
xDeg.enabled = false;
yDeg.enabled = false;
Time.timeScale = 0f;
return (true);
}
}
private void OptionsMenu()
{
if (paused)
{
GUI.Box(new Rect(0, 0, Screen.width, Screen.height), "");
VolumeControl();
if (GUI.Button(new Rect((screenWidth - buttonWidth) * 0.5f, screenHeight * 0.85f, buttonWidth, buttonHeight), "Voltar", font))
{
// go back to the main menu
//this.currentPauseDelegate = pauseMenu;
}
if (Input.GetButtonDown("Pause"))
{
this.currentPauseDelegate = pauseMenu;
}
}
}
private void VolumeControl()
{
GUI.Box(new Rect(0, 0, Screen.width, Screen.height), "");
GUI.Label(new Rect((screenWidth - buttonWidth) * 0.5f, screenHeight * 0.15f, buttonWidth, buttonHeight), "Volume", font);
AudioListener.volume = GUI.HorizontalSlider(new Rect((screenWidth - buttonWidth) * 0.5f, screenHeight * 0.22f, buttonWidth, buttonHeight), AudioListener.volume, 0, 1);
}
}
Answer by Berenger · Jun 01, 2012 at 06:01 AM
The boolean canUse is never used. It should be check in Update when an axis is used. Note that the way your using the coroutine, to wait x second then do something, is the same thing as using Invoke( "Func", x ). No big difference though.
Also, Update isn't a coroutine yet your using an infinite loop inside. Your game must have freeze. That function is called every frames, you don't need the loop in that case.
I actually put up the wrong script since I was still trying to figure it out. Currently it's working like this: if(canUse) { if(Input.GetAxisRaw("Vertical") > 0.5) { selectionIndex--; if(selectionIndex < 0) { selectionIndex = 0 ; } canUse = false; StartCoroutine(InputDelay()); //yield return new WaitForSeconds(keyDelay); }
if(Input.GetAxisRaw("Vertical") < -0.5)
{
selectionIndex++;
if(selectionIndex >= menu.Length)
{
selectionIndex = menu.Length -1;
}
canUse = false;
StartCoroutine(InputDelay());
//yield return new WaitForSeconds(keyDelay);
}
}
IEnumerator InputDelay()
{
print("BEFORE");
yield return new WaitForSeconds(1);
//yield return new WaitForSeconds(keyDelay);
print("AFTER");
canUse = true;
}
The problem is that it never makes canUse true after it's used once.
Your answer
![](https://koobas.hobune.stream/wayback/20220613072226im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unable to click UI Button (Unity 4.6) when TimeScale is 0 1 Answer
Pause Menu Controller Compatibility 0 Answers
Bool wont change... #C 1 Answer
Pause Menu Text Not Rendering 0 Answers
Multiple Cars not working 1 Answer