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. Dismiss Notice

UI buttons not working in main menu

Discussion in 'Scripting' started by dusthound, Jun 1, 2020.

  1. dusthound

    dusthound

    Joined:
    Sep 18, 2018
    Posts:
    98
    So I have run into a bug to do with the main menu in my game. When I start the game, it works fine. All the animations and sound effects are played, all the scene transitions are played, and the levels are loaded when their corresponding buttons are pressed. The problems arise when I return to the main menu using a pause menu built into the levels. When I pause the level and return to the main menu, the buttons are unresponsive. Their animations don't play and they don't load the scenes like their supposed to. To reiterate, if I start the game from the main menu it works fine, the only time it stops working it is when I load the main menu from a level.

    Just to clarify, I am using the OnClick function built into unity to call the method that corresponds to each button. These are custom methods I wrote myself that play the animations and load the scenes. I did some debugging and found that the buttons were still detecting mouse input, but weren't calling the animation triggers or loading the scenes they were supposed to. Strangely, the sound effects that play when the buttons are clicked were still playing while everything else wasn't working. I also found that the bug is only triggered when loading the main menu from a level and not when loading it from the credits scene. Also, when the main menu is loaded, a scene transition is played to smooth the transition from another scene. When loading the main menu from another scene, this transition doesn't play and all I see is a black screen in the game view.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine.SceneManagement;
    4. using UnityEngine;
    5.  
    6. public class TitleScreen : MonoBehaviour
    7. {
    8.     //This is the level that is loaded
    9.     public string GraveyardScene;
    10.  
    11.     //This is the credits scene
    12.     public string CreditsScene;
    13.  
    14.     //These are references to the scene transition animators
    15.     public Animator FadeInAnimator, FadeOutAnimator;
    16.  
    17.     public void OnEnable()
    18.     {
    19.         //This plays a scene transition when the title screen is loaded
    20.         FadeInAnimator.Play("FadeIn");
    21.         Debug.Log("Title screen has been loaded");
    22.     }
    23.  
    24.  
    25.  
    26.     //This method is called when the play button is pressed
    27.     public void PlayButton()
    28.     {
    29.      
    30.         FindObjectOfType<AudioManager>().Play("Click");
    31.         FadeOutAnimator.SetTrigger("FadeOutCalled");
    32.         StartCoroutine(LoadAfterDelay(2.5f, GraveyardScene));
    33.  
    34.         Debug.Log("ButtonClicked");
    35.     }
    36.  
    37.     //this method is called when the credits button is pressed
    38.     public void CreditsButton()
    39.     {
    40.      
    41.         FindObjectOfType<AudioManager>().Play("Click");
    42.         FadeOutAnimator.SetTrigger("FadeOutCalled");
    43.         StartCoroutine(LoadAfterDelay(2f, CreditsScene));
    44.  
    45.         Debug.Log("ButtonClicked");
    46.  
    47.     }
    48.  
    49.     //This method is called when the quit button is pressed
    50.     public void QuitButton()
    51.     {
    52.      
    53.         FindObjectOfType<AudioManager>().Play("Click");
    54.         Application.Quit();
    55.  
    56.         Debug.Log("ButtonClicked");
    57.     }
    58.  
    59.     //this coroutine is used to ensure that the scene transitions have time to play before the levels are loaded
    60.     IEnumerator LoadAfterDelay(float i, string SceneToLoad)
    61.     {
    62.         yield return new WaitForSeconds(i);
    63.         SceneManager.LoadScene(SceneToLoad);
    64.     }
    65. }
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    My silver-bullet guess is that your pause screen sets Time.timeScale = 0 and then you fail to change it back to normal.

    Under this theory, your animations are starting, but don't progress past the first frame because no time passes, and your "LoadAfterDelay" function has a delay of infinity for the same reason.
     
    Ayush273 likes this.
  3. dusthound

    dusthound

    Joined:
    Sep 18, 2018
    Posts:
    98
    I will set the timescale to 1 when the main menu is loaded and see if that helps
     
  4. dusthound

    dusthound

    Joined:
    Sep 18, 2018
    Posts:
    98
    That worked like a charm, thank you so much!!
     
  5. g1_gurjar

    g1_gurjar

    Joined:
    Feb 20, 2021
    Posts:
    1
    Hello!! I am having the same issue. unable to fix it. can you help with the same?
    i don't know where to Time.timeScale = 1.
     
  6. OmarStudios

    OmarStudios

    Joined:
    Apr 14, 2021
    Posts:
    2

    g1_garjar , in any script , Or in the game manager script , Preferred like this: void awake(){time.timeScale = 1;}
    that will work
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,521
    Whilst it's great that you're trying to provide useful information to other devs, please ensure, in the very least, that it's correct otherwise you'll make things worse. In your case, know that the Awake calls starts with a capital letter "A".
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    NONE of the above code will work. You have successfully packed TWO (2) SEPARATE ERRORS into your very first post, and you only posted one line of code. Bravo!

    If you are going to join and post on this forum, please at least post correct code. Otherwise you are simply causing problems for new people who don't know better than to use your broken code example.

    This is the correct place, most likely:

    Code (csharp):
    1. void Awake()
    2. {
    3.   Time.timeScale = 1.0f;
    4. }
    Capitalization is CRITICAL.