Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Game doesn't recognize touch input (testing on Android devices)

Discussion in 'Scripting' started by mangustas, Jun 4, 2014.

  1. mangustas

    mangustas

    Joined:
    Oct 11, 2013
    Posts:
    15
    Hi all. I have created a game recently, which requires the player to touch the screen to start the game. But it didn't recognizes. After I touch the main game screen to start the game, in the debug logcat I'm getting these errors:

    I/Unity (30151): IndexOutOfRangeException: Array index is out of range.
    I/Unity (30151): at GUIManager.StartToPlay () [0x00000] in <filename unknown
    >:0
    I/Unity (30151): at GUIManager.ButtonUp (UnityEngine.Transform button) [0x00
    000] in <filename unknown>:0
    I/Unity (30151): at InputManager.GetClicks () [0x00000] in <filename unknown
    >:0
    I/Unity (30151): at InputManager.Update () [0x00000] in <filename unknown>:0

    I/Unity (30151):
    I/Unity (30151): (Filename: Line: -1)
    I/Unity (30151):


    As you can see, it just doesn't recognize the the clicks (even though I'm not clicking but touching the screen).
    I'm going to show you those functions snippets one by one.

    GUIManager.StartToPlay ():

    Code (CSharp):
    1. //Starts the level
    2.     void StartToPlay()
    3.     {
    4.         //If the main mission list and the shop is hidden, and the level is not starting
    5.         if (!mainMissionHidden || !shopHidden || starting)
    6.             return;
    7.    
    8.         starting = true;
    9.    
    10.         //If the main menu header is not hidden
    11.         if (!mainMenuTopHidden)
    12.         {
    13.             //Hide and disable it
    14.             StartCoroutine(FadeScreen(0.25f, 0));
    15.             StartCoroutine(MoveMenu(mainMenuElements[1].transform, 0, 32, 0.25f, true));
    16.         }
    17.         //If it is hidden
    18.         else
    19.         {
    20.             //Disable it
    21.             EnableDisable(mainMenuElements[1], false);
    22.         }
    23.    
    24.         //Start the level
    25.         LevelManager.Instance.StartLevel();
    26.     }
    GUIManager.ButtonUp (UnityEngine.Transform button):

    Code (CSharp):
    1. //Button Up event
    2.     public void ButtonUp(Transform button)
    3.     {
    4.         //If the playe cant click, return to caller
    5.         if (!canClick)
    6.             return;
    7.    
    8.         //Scale the menu up to it's original position
    9.         Vector3 scale = button.transform.localScale;
    10.         button.transform.localScale = scale * 1.25f;
    11.    
    12.         //Activate the correct function
    13.         switch (button.name)
    14.         {
    15.         .........
    16.         case "PlayTriggerer":
    17.             StartToPlay();
    18.             break;
    19.        
    20.         .........
    21.         }
    22.     }
    GetClicks () and GetTouches():

    Code (CSharp):
    1. //If playing with mouse
    2.     void GetClicks()
    3.     {
    4.         //If we pressed the mouse
    5.         if(Input.GetMouseButtonDown(0))
    6.         {
    7.             //Cast a ray
    8.             ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    9.        
    10.             //If the ray hit something in the set layer
    11.             if (Physics.Raycast(ray, out hit, Mathf.Infinity, mask))
    12.             {
    13.                 //Register it, and send it to the GUI manager
    14.                 button = hit.transform;
    15.                 GUIManager.Instance.ButtonDown(button);
    16.             }
    17.             //If the ray didn't hit a GUI object
    18.             else
    19.             {
    20.                 //Set the button to null, and move the sub up
    21.                 button = null;
    22.                 PlayerManager.Instance.MoveUp();
    23.             }
    24.         }
    25.         //If the click was released
    26.         else if (Input.GetMouseButtonUp(0))
    27.         {
    28.             //If there is no button registered previousely
    29.             if (button == null)
    30.                 //Move the sub down
    31.                 PlayerManager.Instance.MoveDown();
    32.             //If there is a button registered
    33.             else
    34.                 //Send it to the GUI manager
    35.                 GUIManager.Instance.ButtonUp(button);
    36.         }
    37.    
    38.         //Used in testing to reset the status
    39.         /*if (Input.GetKey(KeyCode.P))
    40.         {
    41.             SaveManager.CreateData();
    42.             missionManager.ResetDataString();
    43.         }*/
    44.     }
    45.     //If playing with touch screen
    46.     void GetTouches()
    47.     {
    48.         //Loop through the touches
    49.         foreach (Touch touch in Input.touches)
    50.         {
    51.             //If a touch has happened
    52.             if (touch.phase == TouchPhase.Began && touch.phase != TouchPhase.Canceled)
    53.             {
    54.                 //Cast a ray
    55.                 ray = Camera.main.ScreenPointToRay(touch.position);
    56.            
    57.                 //If the ray hit something in the set layer
    58.                 if (Physics.Raycast(ray, out hit, Mathf.Infinity, mask))
    59.                 {
    60.                     //Register it, and send it to the GUI manager
    61.                     button = hit.transform;
    62.                     GUIManager.Instance.ButtonDown(button);
    63.                 }
    64.                 //If the ray didn't hit a GUI object
    65.                 else
    66.                 {
    67.                     //Set the button to null, and move the sub up
    68.                     button = null;
    69.                     PlayerManager.Instance.MoveUp();
    70.                 }
    71.             }
    72.             //If a touch has ended
    73.             else if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)
    74.             {
    75.                 //If there is no button registered previousely
    76.                 if (button == null)
    77.                     //Move the sub down
    78.                     PlayerManager.Instance.MoveDown();
    79.                 //If there is a button registered
    80.                 else
    81.                     //Send it to the GUI manager
    82.                     GUIManager.Instance.ButtonUp(button);
    83.             }
    84.         }
    85.     }
    Update():

    Code (CSharp):
    1. //Called at every frame
    2.     void Update ()
    3.     {
    4.         if (useTouch)
    5.             GetTouches();
    6.         else
    7.             GetClicks();
    8.     }
    The whole week I'm trying to figure it out, why it's happening. It started when I implemented unsuccessfully implemented Chartboost, but when I deleted everything with what relates to Chartboost to make the project clean like it was before Chartboost implementation. But it stayed the same. I really don't know what is wrong with my code.
    Thank you very much who will read my thread and thank you even more if you could spot my mistakes ir the code.
     
    Last edited: Jun 4, 2014
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    how is useTouch getting set? seems odd that the debug is talking about GetClicks() when it's a touch input...
     
  3. mangustas

    mangustas

    Joined:
    Oct 11, 2013
    Posts:
    15
    Code (CSharp):
    1. (useTouch)
    is set to
    Code (CSharp):
    1. public bool useTouch = false;
    Yes, indeed. It was fine after I tried to implement ChartBoost Ads. After that, it never worked.
    But, today (not so long ago), after one built and export, it was everything fine (touch worked perfectly), but I realised that I'm using same Version Code (I need to change Version Code in case I want to upload a new APK file to Google Play Store), so I needed to change that to the other number. After I changed that, it returned to the old one (without touch), althought I HAVEN'T CHANGED aA SINGLE LINE OF CODE.
    I know, you will start to think what kind of crap I'm talking right now, but it's true.I don't know what's happening with my game. Strange things happening.

    By the way, thank you very much for helping me out, LeftyRighty
     
    Last edited: Jun 4, 2014