x


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);
}

}

more ▼

asked Jun 01 '12 at 05:51 AM

matthew_tavares1 gravatar image

matthew_tavares1
3 6 9 9

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Jun 01 '12 at 06:01 AM

Berenger gravatar image

Berenger
11k 12 19 53

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.

Jun 01 '12 at 06:13 AM matthew_tavares1

Have you tried using the debugger?

Jun 01 '12 at 07:13 AM FormativeTech
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4146
x520
x385
x260
x170

asked: Jun 01 '12 at 05:51 AM

Seen: 424 times

Last Updated: Jun 01 '12 at 07:13 AM