0

I am trying to create mobile input for my Unity game, but no matter what I try, nothing seems to work. I am using c# for all of the coding. When the main menu loads, I use the following function inside of a script inside of a sprite:

void OnMouseDown (){
   SceneManager.LoadScene("gameScreen");
}

And this code works perfectly fine, it successfully loads the screen. My next problem is encountered when I want to get mobile touch input on the next scene.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class MobileTouchInput : MonoBehaviour {

bool supportsMultiTouch;

// Use this for initialization
void Start () {
    supportsMultiTouch = Input.multiTouchEnabled;
    Debug.Log ("MultiTouchSupport: " + supportsMultiTouch);
}

// Update is called once per frame
void Update () {
    int nbTouches = Input.touchCount;
    if (nbTouches > 0) {
        Debug.Log (nbTouches + " touch(es) detected");
        for (int i = 0; i < nbTouches; i++) {
            Touch touch = Input.GetTouch (i);
            Debug.Log ("Touch Index " + touch.fingerId + " detected at position " + touch.position);

            TouchPhase phase = touch.phase;

            switch(phase)
            {
            case TouchPhase.Began:
                Ray screenRay = Camera.main.ScreenPointToRay(touch.position);

                RaycastHit hit;
                if (Physics.Raycast(screenRay, out hit))
                {
                    Debug.Log("User tapped on game object " + hit.collider.gameObject.name);
                }
                break;
        }
    }
}

}

This script is attached to a sprite which also has a 2D Physics Raycaster Script attached to it? I am not sure what it does but I read somewhere that I needed one. I tried the same code with a 2D physics bounding rectangle but that didn't work either :( Upon building the project and running it on an android device, an LGV10 to be exact, it would work with the OnMouseDown() function, load the next scene, and then refuse to work at all with touches on the gameScreen scene. I know this because none of the Debug.Log statements show up when I tap on the sprite that has a raycaster attached to it. Any help would be greatly appreciated, I have been working on this bug for about 2 days and it is becoming quite frustrating to me.

EDIT:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class SpriteDetector : MonoBehaviour, IPointerDownHandler
{
void Start()
{
    addPhysics2DRaycaster();
}

void addPhysics2DRaycaster()
{
    Physics2DRaycaster physicsRaycaster = GameObject.FindObjectOfType<Physics2DRaycaster>();
    if (physicsRaycaster == null)
    {
        Camera.main.gameObject.AddComponent<Physics2DRaycaster>();
    }
}

public void OnPointerDown(PointerEventData eventData)
{
    Debug.Log("Clicked: " + eventData.pointerCurrentRaycast.gameObject.name);
}

//Implement Other Events from Method 1
}

Above is the code for the SpriteDetector class that you created.

Camera picture

Coffee Pot Sprite GameObject

EventSystems picture

Pixelknight1398
  • 537
  • 2
  • 10
  • 33
  • See #7 from the answer on the duplicated question. Since this is a `SpriteRenderer`, `Physics2DRaycaster` must be attached to the Camera. You **don't** need to perform raycast like you did in your uestion. After this you should be using the `OnPointerXXX` functions such as `OnPointerDown, OnPointerClick....See #1 for all the functions. – Programmer Jun 24 '17 at 06:07
  • Tried it, and it still didn't work. Nothing came out of the console. – Pixelknight1398 Jun 24 '17 at 06:28
  • See [this](https://stackoverflow.com/a/44731054/3785314) answer I just provided few hours ago. That's a possible reason. – Programmer Jun 24 '17 at 06:33
  • Still unsuccessful. On computer when I am running on the editor however, the event system positions of the cursor update and eligibleForClick flicks to true when I click. However there is nothing occurring when I launch on an android device. – Pixelknight1398 Jun 24 '17 at 06:41
  • 1.Add **edit** to your question and add the new code you are now using based on my comments. 2.Select that Sprite Object and post a screenshot of it's Inspector settings. 3.Select the camera then post screenshot of it's Inspector. 4.Select the EventSystem Object and post the screenshot of its Inspector . With these I can see your scene setup and be able to help you more. – Programmer Jun 24 '17 at 06:48
  • Added edits with the desired code and screenshots. What I also just tried, is moving the SpriteDetector script from the actual camera to the sprite that I need receiving input, but that didn't do anything either >.> – Pixelknight1398 Jun 24 '17 at 07:00
  • lol `SpriteDetector` script with the `OnPointerDown` function should be attached to the GameObject you want to detect click on which is the "coffeepot" **not** the camera. What you need to attach to the camera is the `Physics2DRaycaster` script but don't worry about that because the `addPhysics2DRaycaster` function in the `SpriteDetector` script will do that for automatically when you attach it to the "coffeepot" Object. Please remove it from the camera. – Programmer Jun 24 '17 at 07:07
  • What you pointed out, I had already completed and edited my previous comment to show such that. After attaching the SpriteDetector script to the coffeepot object, still nothing occurs. Sorry. – Pixelknight1398 Jun 24 '17 at 07:13
  • Huh? Your last screenshot does not show the `SpriteDetector` script attached to it. By the way, the coffee collider is disabled. You have to enable it. If that does not work, remove the edited images and put new ones based on my last comment + this one. – Programmer Jun 24 '17 at 07:18
  • I have updated the screenshots to show the changes I made to the coffee pot object, and the camera. I moved the `SpriteDetector` script, and enabled the coffee pot collider. Currently, when I run the app on the unity editor and click the coffee pot sprite, it shows the Debug.Log message, however when I run it on my android device and tap the coffee pot sprite, no message appears. I have to admit this is quite frustrating – Pixelknight1398 Jun 24 '17 at 07:26
  • Ok so here is what I have done. I added a second line of code to the OnPointerDown function in the script as `SceneManager.LoadScene("mainMenu")` and it does successfully execute this line of code on the android device. Though not the Debug.Log statement. Is Unity not tracking the input of the device after install? I am new to Unity, and accustomed to developing in Android Studio, which follows everything that occurs on the device at run time. Sorry for this confusion I just expected the Debug.Log statement to show up in the console. – Pixelknight1398 Jun 24 '17 at 07:34
  • No it's not suppose to show in the console. I didn't even know this is what you are doing. Anyways, don't use raycast to detect which object is clicked. Use that method you edited in your question. If you want to see log on Android devices see [this](https://stackoverflow.com/a/44690501/3785314). If you want the Android log to show on the Editor, you have to run the app on the Editor with Unity Remote....see [this](https://stackoverflow.com/a/39107871/3785314) for that. – Programmer Jun 24 '17 at 07:39

0 Answers0