Search Unity

UI buttons don't work on Android build but work in editor

Discussion in 'Scripting' started by Robster95, Sep 12, 2022.

  1. Robster95

    Robster95

    Joined:
    Jul 3, 2019
    Posts:
    154
    I have a problem where the player is not able to interact with the buttons on the main menu while playing on a build for android devices.

    I have the main menu loaded additively on the main game (a small hyper casual mobile game) and in editor and other builds the player is able to click on the buttons and make them do their respective actions.

    However, on my android build of the game nothing happens when the player clicks on the buttons.
    I have all of the UI buttons on a canvas, have an event system component in the scene and have the input settings set to both new and legacy input systems.

    Does anyone know how to fix this problem? If so, I would appreciate any help.

    Thank you
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Since you mention additive loading, make sure that nothing is blocking your UI on mobile. Now, I know you say it works in editor, but let's say you have your UI on layer 0 and something else on layer 0. Technically they both share the same layer, but that isn't really possible. They can't occupy the same space, so one is placed over the other. Which could mean your UI is placed over the other in Editor, but not in the build.

    Next, check if you are getting errors. Use logcat (within Unity) to see if any errors are coming up. Add some debug messages, do those appear? If you are hitting errors, your code may not execute.
     
  3. Robster95

    Robster95

    Joined:
    Jul 3, 2019
    Posts:
    154
    I appreciate the response. I'll definitely try it. The problem with this however is I'm using an old android work phone to test the build and I'm not able to connect to the internet to download the unity remote app to test with. Is there a way I can get debug statements on the screen while running the build?

    I have a working windows version and webGL version of this game that doesn't have the UI button. I only noticed this on the android build.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    Yaaas...
    adb logcat
    ... and nowadays there's even a plugin to make it spew into the Unity console window.
     
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Yeah, I wasn't meaning the remote app. Just leave the device plugged into your computer and install logcat in the package manager. You can use command line also, but I've been there, done that. If you can, use the logcat in Unity. It's much nicer. Also, you can install a newer version of Unity if for some reason logcat isn't available in your version, create an empty project and use logcat through the empty project.

    https://assetstore.unity.com/packages/tools/integration/log-viewer-12047 Also, something like this can work as well. But I suggest learning logcat because it can handle crashes and other things log viewer can't.
     
  6. Robster95

    Robster95

    Joined:
    Jul 3, 2019
    Posts:
    154
    Ok. I'll try looking into it. Just to make sure this would be able to help me this these problems before making the build right? Also, Brathnann I looked into the adding the scene additively and everything in the game is on the default layer except the UI. The UI is the only thing on the UI layer
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    Just connecting it won't help.

    But maybe it's throwing an error that could give you a clue.

    Or maybe it's not.

    If it's not, now at least you can add Debug.Log() calls to figure out what is going on. Here's more to think on it:

    You must find a way to get the information you need in order to reason about what the problem is.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling
    Debug.Log()
    statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as
    Debug.Log("Problem!",this);


    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

    When in doubt, print it out!(tm)

    Note: the
    print()
    function is an alias for Debug.Log() provided by the MonoBehaviour class.
     
  8. Robster95

    Robster95

    Joined:
    Jul 3, 2019
    Posts:
    154
    So I've tried looking at the problem with debugs and printing text on the screen to see exactly where the code runs and fails. the problem I've found is that the code isn't getting ran at all in the Android build. In edittor everything runs perfectly fine with no problems at all. The other builds of the game I have are perfectly fine too but for android specifically it is having this problem. It may be a mobile problem but I haven't been able to check the game on an official iOS build.
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    Yep, such a commonplace problem that it's the first on my bucket list above:

    So now you gotta figure out why.

    Review what is required for code to run (basically "on an enabled GameObject in a loaded scene")

    Construct experiments with other test code "Hello I am alive!" on other objects. Do they run?

    Bisect towards a solution.