I
I
Ilya_malet2021-10-19 22:47:05
Unity
Ilya_malet, 2021-10-19 22:47:05

Why don't the UI buttons work?

I am making a UI interface for the game, subscribing to button events using AddListener(). When you click on the "Play" button, nothing happens, although the button is pressed. What could it be?

public class StartScreen : MonoBehaviour
{
    [SerializeField] private CanvasGroup CanvasGroup;
    [SerializeField] private Button StartButton;
    [SerializeField] private Button QuitButton;

    public event UnityAction StartButtonClick;
    public event UnityAction QuitButtonClick;

    private void OnAnable()
    {
        StartButton.onClick.AddListener(OnStartButtonClick);
        QuitButton.onClick.AddListener(OnQuitButtonClick);
    }

    private void OnDisable()
    {
        StartButton.onClick.RemoveListener(OnStartButtonClick);
        QuitButton.onClick.RemoveListener(OnQuitButtonClick);
    }

    public void Open()
    {
        CanvasGroup.alpha = 1;
        StartButton.interactable = true;
        QuitButton.interactable = true;
    }

    public void Close()
    {
        CanvasGroup.alpha = 0;
        StartButton.interactable = false;
        QuitButton.interactable = false;
    }

    private void OnStartButtonClick()
    {
        Debug.Log("1");

        StartButtonClick?.Invoke();
    }

    private void OnQuitButtonClick()
    {
        QuitButtonClick?.Invoke();
    }
}

public class Game : MonoBehaviour
{
    [SerializeField] private StartScreen _startScreen;
    [SerializeField] private GameOverScreen _gameOverScreen;
    [SerializeField] private Player _player;

    private void OnAnable()
    {
        _startScreen.StartButtonClick += OnStartButtonClick;
        _startScreen.QuitButtonClick += OnQuitButtonClick;

        _gameOverScreen.RestartButtonClick += OnRestartButtonClick;
        _gameOverScreen.QuitButtonClick += OnQuitButtonClick;

        _player.GameOver += OnGameOver;
    }

    private void OnDisable()
    {
        _startScreen.StartButtonClick -= OnStartButtonClick;
        _startScreen.QuitButtonClick -= OnQuitButtonClick;

        _gameOverScreen.RestartButtonClick -= OnRestartButtonClick;
        _gameOverScreen.QuitButtonClick -= OnQuitButtonClick;

        _player.GameOver -= OnGameOver;
    }

    private void Start()
    {
        _gameOverScreen.Close();
        Time.timeScale = 0;
        _startScreen.Open();
    }

    private void OnStartButtonClick()
    {
        _startScreen.Close();
        Time.timeScale = 1;
    }

    private void OnRestartButtonClick()
    {
        SceneManager.LoadScene(0);
        _gameOverScreen.Close();
        Time.timeScale = 1;
    }

    private void OnQuitButtonClick()
    {
        Application.Quit();
    }

    private void OnGameOver()
    {
        Time.timeScale = 0;
        _gameOverScreen.Open();
    }


}


There is also a GameOverScreen class, but it is similar to the StartOverScreen class, only the Restart button. The Game component is hung on the camera, and GameOverScreen and StartOverScreen on Panel and CanvasGroups. I checked many times and can't find anything.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Almost, 2021-10-20
@Ilya_malet

Change the function name from OnAnable to OnEnable

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question