Detect object name on Touch Event

Hi to everybody…
I’ve little problem… I’ve implement this script for detect the name of object that i’ve touch:

using UnityEngine;

using System.Collections;



public class DetectTouch : MonoBehaviour {

    

    private Ray touchRay;

    private RaycastHit touchHit;

    private GUIText messages;

    private GameObject oggetto;

    private Camera inputCamera;

    

    void Awake(){

        if(!inputCamera){

            inputCamera = Camera.main;

            oggetto = GameObject.Find("GUITesto");

            messages = (GUIText) oggetto.GetComponent("GUIText");

        }

    }

    

    // Use this for initialization

    void Start () {

    

    }

    

    // Update is called once per frame

    void Update () {

        UpdateTouchInput();

    }

    

    void UpdateTouchInput(){

        if(Input.touchCount == 1){

            Touch touch = Input.touches[0];

             if(touch.phase == TouchPhase.Began){

                touchRay = inputCamera.ScreenPointToRay(touch.position);

                if(Physics.Raycast(touchRay,out touchHit)){

                    switch(touchHit.collider.name){

                        default:

                            messages.text = touchHit.collider.name;

                            break;

                    }

                }

            }

        }

    }

}

it’s correctly function.
In another project i’ve use the prefab “TapToMove Controls” but when i touch the sphere for example, the script is not function… why???
Ps. This script is associate to the sphere…

Please help me…:face_with_spiral_eyes:

try this and see if you get a result.

touchRay = inputCamera.ScreenPointToRay(touch.position);
foreach( RaycastHit hit in Physics.RaycastAll(touchRay) ) {
  Debug.Log( hit.transform.name );
}

If you have a compound collider (ie, a set of objects with different colliders all attached to a parent object with a rigidbody) then the code you have posted will not necessarily return the name of the root object. If you use something like

messages.text = touchHit.transform.root.name;

…then you will always get the name of the root rather than one of the child objects.