Search Unity

Android game is crashing

Discussion in 'Android' started by herbie, Nov 6, 2013.

  1. herbie

    herbie

    Joined:
    Feb 11, 2012
    Posts:
    237
    Hi,

    In my Android game I have a start screen with some GUI touch buttons where you can choose the level you want to play.
    When you wipe with your fingers over the screen as if you are cleaning it (so you are touching several GUI buttons at once), the game is crashing. The GUI buttons doesn't react anymore.

    I did already something like this to ensure that only one button is hit:

    Code (csharp):
    1. function OnGUI ()
    2. {
    3.     if (Input.touchCount > 1  Input.GetTouch(0).phase == TouchPhase.Ended)
    4.     {
    5.         touch = 0;
    6.     }
    7.    
    8.     if (GUI.Button(Rect(10,10,10,10),"", styleButton1)  touch == 0)
    9.     {
    10.         touch = 1;
    11.         // etc.
    12.     }
    13. }
    But that doesn't work. It is still crashing.

    Does anybody know what I can do about it?
     
    Last edited: Nov 7, 2013
  2. Agent_007

    Agent_007

    Joined:
    Dec 18, 2011
    Posts:
    899
    What does adb logcat complain when crash occurs?
     
  3. herbie

    herbie

    Joined:
    Feb 11, 2012
    Posts:
    237
    When I touch the screen with 3 or more fingers at the same time, the GUI buttons does not work anymore.
    Then after I touched the screen with 2 fingers at the same time, the GUI buttons are working again.

    So the game is not crashed but I think it has something to do with the Touch Count Bug http://forum.unity3d.com/threads/111148-Touch-Count-Bug

    By the way, this problem only occurs on my HTC phone.
    My Galaxy Note is working fine.
     
  4. herbie

    herbie

    Joined:
    Feb 11, 2012
    Posts:
    237
    Angry birds also has this problem on my HTC One.
    But there you have to touch with one finger to make te GUI buttons active again. That's a more natural response.

    How do I get some kind of reset of the GUI buttons when you tap on the screen with one finger?

    edit:
    I also found a game on Google play that has the exact problem as I have:
    https://play.google.com/store/apps/details?id=com.CubeProduction.TapToBreak
    (touch the screen with 3 or more fingers at the same time, the GUI buttons does not work anymore.
    Then touch the screen with 2 fingers at the same time, the GUI buttons are working again).

    I also installed the game Bad Piggies, which is made with Unity, and this game does not has the problem.
    So there must be a solution.
     
    Last edited: Nov 7, 2013
  5. herbie

    herbie

    Joined:
    Feb 11, 2012
    Posts:
    237
    I installed Unity Remote on my HTC.
    I also put this in the code:
    Code (csharp):
    1. test = Input.touchCount;
    Now I can see the following:

    put finger on screen --> test = 1
    remove finger --> test = 0
    put 2 fingers on screen --> test = 2
    remove fingers --> test = 0
    put 3 or more fingers on screen --> test = 1
    remove finger --> test = 1

    So something goes wrong with Input.touchCount.
     
  6. jvil

    jvil

    Joined:
    Jul 13, 2012
    Posts:
    263
    Don't use OnGUI to handle touch, use Update. Avoid OnGUI on mobile. Also, you should consider alternatives such as EZGUI or NGUI.
     
  7. herbie

    herbie

    Joined:
    Feb 11, 2012
    Posts:
    237
    Thanks for reply.

    I know that it was better to avoid OnGUI on mobile about 2 years ago. But is it still better to avoid it nowadays?
    You can play my game very good on a phone. It's not slow and it does not crash.
    Except the issue with three fingers on a HTC phone.
    I did the same with my Galaxy note and that's working fine.

    When you touch the screen with three fingers, the Input.touchCount becomes 1 and stays 1 if you remove your fingers.
    I know Input.touchCount is read only but is there a way to reset Input.touchCount?
     
  8. jvil

    jvil

    Joined:
    Jul 13, 2012
    Posts:
    263
    Yes, OnGUI can be expensive on mobile. Even an empty OnGUI function might drop your framerate.

    As rule of thumb always make sure don't have unnecessary drawcalls. My advice is use OnGUI for prototyping, but not for shipping your game.
     
  9. herbie

    herbie

    Joined:
    Feb 11, 2012
    Posts:
    237
    Despite the fact that you can play my game with OnGUI very good on mobile, I decided to get rid of the OnGUI to avoid other problems on other phones.

    My 2D game has no more than 90 drawcalls and that's for the OnGUI menu with 24 GUI Buttons (you can choose one of the 24 levels to play).
    The game itselfs has no more than about 50 drawcalls.