- Home /
Figured out myself. Might be a Unity 5 bug, actually.
Can't enable/disable canvas on Android device
I'm trying to create a pause menu. When I press the escape button (back button on Android device) the canvas with menu should activate. When I do this on PC in Unity 5 - it works great. When I build and run this app on my Android phone - it doesn't works. I've tried two methods. The first one is:
public class GameController : MonoBehaviour
{
private Canvas pauseMenu;
private void Start()
{
GameObject pauseMenuobject = GameObject.FindWithTag("PauseMenu");
if (pauseMenuobject != null)
pauseMenu = pauseMenuobject.GetComponent<Canvas>();
if (pauseMenuobject == null)
Debug.Log("Cannot find Canvas");
pauseMenu.gameObject.SetActive(false);
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
if (!pauseMenu.gameObject.activeInHierarchy)
pauseMenu.gameObject.SetActive(true);
else if (pauseMenu.gameObject.activeInHierarchy)
pauseMenu.gameObject.SetActive(false);
}
}
And the other one is:
public class GameController : MonoBehaviour
{
private Canvas pauseMenu;
private void Start()
{
GameObject pauseMenuobject = GameObject.FindWithTag("PauseMenu");
if (pauseMenuobject != null)
pauseMenu = pauseMenuobject.GetComponent<Canvas>();
if (pauseMenuobject == null)
Debug.Log("Cannot find Canvas");
pauseMenu.enabled = false;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape)){
if (pauseMenu.enabled == false)
pauseMenu.enabled = true;
else if (pauseMenu.enabled == true)
pauseMenu.enabled = false;
}
}
Both work in the Unity 5, but when I build and run it on Android - it doesn't works. Even more, on android it doesn't even disables the canvas in void Start() nor it passes the if () in the void Update().
What am I doing wrong? Is this type of disabling/enabling works only in Unity 5 and does not works when you build it? Or is it because I launch it on the Android? What should I do? Thanks in advance.
Somehow, reloading the Unity 5 - fixed this problem. I'm not sure, but I guess it has something to do with tags. When I was making a tag for my menu canvas I made a mistake, then deleted this tag, and created a new one. When I deleted the first one I had a message "There are removed tags. They will be removed from this list the next time the project is loaded".
Though, further on I used only the correct, second one, tag, somehow this deleted tag seems to effect the app, when it was build and launched on Android device.