Search Unity

"Press H to show/hide menu makes the menu flicker when I press it once"

Discussion in 'Scripting' started by realpero215, Oct 6, 2019.

  1. realpero215

    realpero215

    Joined:
    Oct 6, 2019
    Posts:
    3
    So, I managed to work around the flickering thing with my pause using coroutines but for some reason I can't do it with my "press H to hide/show menu", when I press H, it just starts enabling and disabling it every frame.

    Code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.SceneManagement;
    6.  
    7. public class uiManager : MonoBehaviour
    8. {
    9.     // Text
    10.     public Text speedText;
    11.     public Text staminaText;
    12.  
    13.     // Ball
    14.     public GameObject ball;
    15.     private bball bscript;
    16.  
    17.     // Help menu
    18.     public Text hText;
    19.     public Image hBack;
    20.  
    21.     // Other
    22.     bool paused = false;
    23.     bool helpIsHidden = false;
    24.     public float inputDelay = 5.0f;
    25.  
    26.     public IEnumerator getPause()
    27.     {
    28.         for (;;)
    29.         {
    30.             if (paused)
    31.                 Time.timeScale = 0;
    32.             else if (!paused)
    33.                 Time.timeScale = 1;
    34.             yield return new WaitForSeconds(inputDelay);
    35.         }
    36.     }
    37.  
    38.     public IEnumerator getHelp()
    39.     {
    40.         for (;;)
    41.         {
    42.             if (helpIsHidden)
    43.             {
    44.                 hText.enabled = false;
    45.                 hBack.enabled = false;
    46.             }
    47.             else
    48.             {
    49.                 hText.enabled = true;
    50.                 hBack.enabled = true;
    51.             }
    52.             yield return new WaitForSeconds(inputDelay);
    53.         }
    54.     }
    55.  
    56.     void Update()
    57.     {
    58.         bscript = ball.GetComponent<bball>();
    59.         speedText.text = "Speed: " + ((double)(float)ball.GetComponent<Rigidbody>().velocity.magnitude).ToString("F4") + " m/s";
    60.         if (bscript.stamina < 0) bscript.stamina = 0;
    61.         staminaText.text = "Stamina: " + ((double)bscript.stamina).ToString("F0");
    62.  
    63.         // GetKeys
    64.  
    65.         StartCoroutine(getPause());
    66.         StartCoroutine(getHelp());
    67.  
    68.         if (Input.GetKeyDown(KeyCode.P))
    69.         {
    70.             // Pause
    71.             paused = !paused;
    72.         }
    73.  
    74.         if (Input.GetKeyDown(KeyCode.R))
    75.         {
    76.             // Restart level
    77.             SceneManager.LoadScene("game", LoadSceneMode.Single);
    78.         }
    79.  
    80.         if (Input.GetKey(KeyCode.H))
    81.         {
    82.             // Show/hide help menu
    83.             helpIsHidden = !helpIsHidden;
    84.         }
    85.  
    86.         if (Input.GetKey("escape"))
    87.         {
    88.             // Exit game
    89.             Application.Quit();
    90.         }
    91.     }
    92. }
    93.  
    Any fixes to this or optimizations for my code? Thank you in advance.
     
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    A few issues here, mainly the empty for loops, you are starting a coroutine every frame and you are using getkeyfor your H input when you meant to use getkeydown