Search code examples
unity-game-enginetouch

How to print when touching only gameobject?


I want to print held when I touch only the gameObject

public GameObject Gameobject;   
void Update () {
        if (Input.GetButton("Fire1"))
        {
            print("held");
        }
    }

So this code prints held when I even touch outside the gameobject.I added the script to the gameobject.The answer must work on android too.


Solution

  • There is event called OnMouseOver()

    void OnMouseOver()
    {
        if (Input.GetAxis("Fire1"))
            {
                print("held");
            }
    }
    

    For Android is different, here you have a link:

    http://answers.unity3d.com/questions/610440/on-touch-event-on-game-object-on-android-2d.html

    They explain how to get the position of the touch and shoot a ray to know if it impacts your object.