Search Unity

Touch Detection on Image

Discussion in 'UGUI & TextMesh Pro' started by The_Devil, Jul 19, 2015.

  1. The_Devil

    The_Devil

    Joined:
    Jun 6, 2015
    Posts:
    36
    Hi. I am very new to unity so please excuse any silly/stupid errors. I am trying to detect touch on an Image that has been added to a Panel under the Canvas. I have added a sphere collider to the image and in the update function have written the following:

    if (Input.GetMouseButtonDown("0"))
    {
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    if (Physics.Raycast(ray, outhit))
    {
    Debug.log(hit.transform.gameObject.tag);
    }​
    }

    The problem is, it never flow never gets to the debug log.Any idea what Im doing wrong?
     
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    899
    Why don't you just make it a button?
     
  3. The_Devil

    The_Devil

    Joined:
    Jun 6, 2015
    Posts:
    36
    I can't make it a button as I have multiple images and I should be able to select multiple objects at one go. Like touch on one and then move my finger/mouse pointer over the others and select them as I move across them.
     
  4. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    899
    OK, then you want to add an "Event Trigger" and use "Add New Event Type" to add a "Pointer Down" event handler.
     
    cuongnmx likes this.
  5. The_Devil

    The_Devil

    Joined:
    Jun 6, 2015
    Posts:
    36
    Thanks for the tip. Will try that out.
     
  6. xainulabdeen

    xainulabdeen

    Joined:
    Apr 10, 2018
    Posts:
    5
    Select Image from Canvas Go
    AddComponent > EventTrigger > AddNewEventType > OnpointerDown

    Then drag the scripting object that holds the function or trigger on anythings
    Hope it Helps
     
    cuongnmx likes this.
  7. cyberIndia

    cyberIndia

    Joined:
    Aug 17, 2014
    Posts:
    8
    Works like a charm
    Thank you
     
  8. Deleted User

    Deleted User

    Guest

    Thnak you xainulabdeen, You save my day