Answer the question
In order to leave comments, you need to log in
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();
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question