Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by matthew_tavares1 · Jun 01, 2012 at 05:51 AM · c#controllermenupausewaitforseconds

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

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image matthew_tavares1 · Jun 01, 2012 at 06:13 AM 0
Share

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.

avatar image FormativeTech · Jun 01, 2012 at 07:13 AM 0
Share

Have you tried using the debugger?

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges