- Home /
UI Button not correctly working
Hello, i'm new in Unity and trying to make an UI for my game. The UI is simple ,when i'm pressing "Escape" it displays the canvas and if i'm pressing "Escape" or click on the button "Return" it goes back to the game.
I freeze the game with Time.scaleTime = 0;
while the canvas is on, but when i go back with the button it does not work and stay freeze, but with the key it works. If i set a Debug.Log
it's only triggered with the key.
Can somebody explain why the button is not working ? Note : The canvas is disabled when the button is pressed
Here's the code i use :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManager : MonoBehaviour
{
private bool _playing;
public GameObject canvas;
// Start is called before the first frame update
void Start()
{
_playing = true;
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape)) {
UpdateUI();
}
}
public void UpdateUI()
{
_playing = !_playing;
canvas.SetActive(!_playing);
Time.timeScale = (_playing ? 1 : 0);
}
}
//using System.Collections;
//using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class BtnScriptGame : MonoBehaviour
{
private Button _btn;
private GameManager _gm;
public int btnType;
// Start is called before the first frame update
void Start()
{
_btn = GetComponent<Button>();
_gm = GameObject.Find("GameManager").GetComponent<GameManager>();
switch (btnType)
{
case 0:
_btn.onClick.AddListener(ResumeGame);
break;
case 1:
_btn.onClick.AddListener(ResetRoom);
break;
default:
_btn.onClick.AddListener(QuitRoom);
break;
}
}
private void ResumeGame()
{
_gm.UpdateUI();
}
private void ResetRoom()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
private void QuitRoom()
{
SceneManager.LoadScene("Scenes/title_screen");
}
}
On line 21, have you checked that it runs?
_btn.onClick.AddListener(ResumeGame);
Okay i feel really stupid and thanks for making me realize it xD Because clicking on the button was correctly loading the scene i didn't make sure to check it. I jus attached the wrong script to it so it was launching the scene but not with the correct method.
Sorry for bothering you !
Your answer
Follow this Question
Related Questions
canvas group breaks button 0 Answers
My UI buttons work fine in editor but when I exported the game on Android, only some of them works. 0 Answers
Use Canvas Buttons as Input Axis 1 Answer
Buttons in UI Scroll Rect 2 Answers
Call Functions On Player From Canvas Elements In a Multiplayer Game 1 Answer