Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

UI Freezes Game for a Moment When First Toggled

Discussion in 'Scripting' started by Wilelle, Apr 4, 2016.

  1. Wilelle

    Wilelle

    Joined:
    Jun 16, 2015
    Posts:
    93
    (I don't know if this is belongs in the scripting category or the UI category, so I'm sorry if this is the wrong place.)

    When "X" is pressed in my game, the game pauses (timescale is set to 0) and a menu is opened. Press "X" again and it closes and resets timescale to 1. This works fine in the Editor, but in a Build the game freezes for a moment when "X" is pressed before opening the menu. However, it works fine when closing it, as well as opening it after the first time. Any idea why this might be?

    This is the script that toggles the menu:
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var player : GameObject;
    4. var statMenu : GameObject;
    5. var healthBar : GameObject;
    6.  
    7. function Start ()
    8. {
    9.     player = GameObject.Find("Player");
    10. }
    11.  
    12. function Update ()
    13. {
    14.     if (Input.GetKeyDown(KeyCode.X))
    15.     {
    16.         if (statMenu.activeSelf == false)
    17.         {
    18.             statMenu.SetActive(true);
    19.             healthBar.SetActive(false);
    20.             Time.timeScale = 0;
    21.         }
    22.         else if (statMenu.activeSelf == true)
    23.         {
    24.             statMenu.SetActive(false);
    25.             healthBar.SetActive(true);
    26.             Time.timeScale = 1;
    27.         }
    28.     }
    29. }
    30.  
     
  2. Zaflis

    Zaflis

    Joined:
    May 26, 2014
    Posts:
    438
    Do you have any scripts under statMenu object? Maybe they're only that point doing their first Start() after being enabled.
     
    Wilelle likes this.
  3. Wilelle

    Wilelle

    Joined:
    Jun 16, 2015
    Posts:
    93
    That was indeed the issue. Now I've made sure all the sub-scripts get a moment to load before the game starts and everything works like it should. Thanks!