Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Touch does not work to click a button which clicks fine on the Mac.

Discussion in 'Scripting' started by BevvyofFun, Jan 21, 2021.

  1. BevvyofFun

    BevvyofFun

    Joined:
    Aug 19, 2020
    Posts:
    25
    I am trying to make touch work when I transfer my project to my iPad, but, so far, no go. Can someone please look at my code and spot what's going wrong? This code is attached to a button.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.SceneManagement;
    6. using UnityEngine.EventSystems;
    7. using UnityEngine.iOS;
    8.  
    9. public class NewGame : MonoBehaviour
    10.  
    11.      {  public void Update()
    12.     {
    13.      
    14.         if((Input.GetMouseButtonDown(0)) && (gameObject.name == "NewGame"))  {
    15.    
    16.               SceneManager.LoadScene("Home");
    17.  
    18.      } else if(Input.touchSupported)
    19.     {
    20.         //public static Touch GetTouch(int index);
    21.         if (Input.touchCount > 0)
    22.         {
    23.             Touch touch = Input.GetTouch(0);
    24.          if ((touch.phase == TouchPhase.Began) && (gameObject.name == "NewGame")){
    25.         SceneManager.LoadScene(1);
    26.  
    27. }
    28. }
    29. }
    30.  
    31. }
    32. }
    33.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    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 are the values of the variables involved? Are they initialized?

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

    Alternately, you can look at how I abstracted both touch and mouse into a single unified array of objects that I call
    MicroTouch
    ... that way my code doesn't care if it is working with mouse or touch, it just works.

    https://github.com/kurtdekker/proxi...buttons/Assets/ProximityButtons/MicroTouch.cs

    The above project is available in its entirety at:

    proximity_buttons is presently hosted at these locations:

    https://bitbucket.org/kurtdekker/proximity_buttons

    https://github.com/kurtdekker/proximity_buttons

    https://gitlab.com/kurtdekker/proximity_buttons

    https://sourceforge.net/projects/proximity-buttons/
     
    BevvyofFun likes this.
  3. BevvyofFun

    BevvyofFun

    Joined:
    Aug 19, 2020
    Posts:
    25
    Well, it's the dreaded out of bounds, for GetTouch. I'm not sure how to correct this, and I really just want the simplest code for this button click event, since there is only one screen with these buttons for New Game, Save, Load and Quit. Any ideas for a simple fix?

    Bev


    ArgumentException: Index out of bounds.


    at UnityEngine.Input.GetTouch_Injected (System.Int32 index, UnityEngine.Touch& ret) [0x00000] in <00000000000000000000000000000000>:0

    at UnityEngine.Input.GetTouch (System.Int32 index) [0x00000] in <00000000000000000000000000000000>:0

    at NewGame.Update () [0x00000] in <00000000000000000000000000000000>:0
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Dreaded? It's like a typo 99.99% of the time. It's the second most common error around (besides nullref).

    Here are some notes on IndexOutOfRangeException and ArgumentOutOfRangeException:

    http://plbm.com/?p=236
     
  5. BevvyofFun

    BevvyofFun

    Joined:
    Aug 19, 2020
    Posts:
    25
    Hi Kurt,

    I know that out of bounds means it's out of the range. The question is what in the code is out of bounds. For the target device, I have tried 0 or 1. Neither worked and both got the same error in Xcode, no error in Unity. I don't know if the error is coming from Unity information or my specific code. Judging from the Xcode information, it seems to state when it followed the code to GetTouch from UnityEngine it reached an error. Am I interpreting this wrong?
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    If the code in question is the code posted at the top, it won't even try to get the first touch unless you at least have more than one, so that's fine. Is something else calling GetTouch? Turn this code off and find out.

    Unrelated to that, I'm not sure why you're doing all the gameObject.name checking... what's the point? Do you expect this script to live on some other GameObject that is NOT named "NewGame" ? I would stay away from a design like that. Instead make a different script on that other GameObject.

    Also unrelated, I would also stay away from the numeric version of LoadScene and always use the string version.

    To me the above code should only need to be:

    Code (csharp):
    1. using UnityEngine;
    2. using UnityEngine.SceneManagement;
    3.  
    4. public class NewGame : MonoBehaviour
    5. {
    6.     void Update ()
    7.     {
    8.         if (Input.GetMouseButtonDown(0))
    9.         {
    10.             SceneManager.LoadScene("Home");
    11.         }
    12.         if (Input.touchCount > 0)
    13.         {
    14.             var touch = Input.GetTouch(0);
    15.             if (touch.phase == TouchPhase.Began)
    16.             {
    17.                 // I recommend staying away from the numeric overload.
    18.                 // Reason: if the numbers change you will silently fail.
    19.                 SceneManager.LoadScene(1);
    20.             }
    21.         }
    22.     }
    23. }

    Any reason you don't just use the Unity UI system and make a full screen invisible button and wait or it to be pressed? Far simpler than wrangling all this code!!!
     
    BevvyofFun likes this.
  7. BevvyofFun

    BevvyofFun

    Joined:
    Aug 19, 2020
    Posts:
    25
    Thank you. I do have only one button that I'm working on now, but I took your suggestion to try it without the gameObject reference and Xcode no longer throw the out of bounds error. Instead the build fails due to out of memory to load the game. I will have to see what's up with this. Thanks again for your help. At least it identified the problems.
     
  8. BevvyofFun

    BevvyofFun

    Joined:
    Aug 19, 2020
    Posts:
    25
    Xcode finally told me that some of the Unity files were corrupted and should be deleted.