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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Return the name of Button with Raycast by clicking the mouse.

Discussion in 'UGUI & TextMesh Pro' started by Djfeeler, Jun 22, 2015.

  1. Djfeeler

    Djfeeler

    Joined:
    Oct 11, 2014
    Posts:
    17
    Hello,

    I would like to know how to return the name of a button by clicking the mouse with a raycast.

    Thanks in advance.
     
  2. Djfeeler

    Djfeeler

    Joined:
    Oct 11, 2014
    Posts:
    17
    I found a response :

    Code (CSharp):
    1.  
    2. PointerEventData pointer = new PointerEventData(EventSystem.current);
    3. pointer.position = Input.mousePosition;
    4.      
    5. List<RaycastResult> raycastResults = new List<RaycastResult>();
    6. EventSystem.current.RaycastAll(pointer, raycastResults);
    7.      
    8. if(raycastResults.Count > 0)
    9. {
    10.       /*if(raycastResults[0].gameObject.name == "Background")
    11.         print("You touched Background");*/
    12.  
    13.        print("Object : " + raycastResults[0].gameObject.name);
    14.  }