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. Dismiss Notice

TextMesh Pro How do you register a click on a TMP text?

Discussion in 'UGUI & TextMesh Pro' started by SimRuJ, May 11, 2021.

  1. SimRuJ

    SimRuJ

    Joined:
    Apr 7, 2016
    Posts:
    247
    Hello everyone,

    I've got a regular TextMeshPro text GO (stand-alone, without canvas) and want to write to console when someone clicks on it. I already added a "ClickText" script to the GO but can't get any of the functions to trigger. What I've tried so far:

    1. Implement "IPointerClickHandler" in "ClickText", with "OnPointerClick(PointerEventData eventData)" - didn't work.
    2. Add a "PointerClicker" event trigger for the GO (through the editor) and set it to call "ClickText.onClick()" function - didn't work.
    3. "OnMouseDown" - didn't work (only meant for GameObjects with an actual collider afaik)

    How do you register clicks on a TMP text (in Unity 2021.1.6f1)?
     
    Last edited: May 12, 2021
  2. SimRuJ

    SimRuJ

    Joined:
    Apr 7, 2016
    Posts:
    247
    I added a MeshCollider and ticked "Is Trigger" and now "OnMouseDown" is called but the other two versions (IPointerClickHandler & Event) still don't work. Any guesses why?
     
  3. Kheremos

    Kheremos

    Joined:
    Dec 13, 2017
    Posts:
    17
    This is an old post, but someone else might have this issue, so...

    I don't think the GO has a mesh or any inherent interactable component, so even though you add the event, it doesn't have anything to trigger from. When I was attempting what you were, I added the event to the Text Object itself and it worked.
     
  4. SimRuJ

    SimRuJ

    Joined:
    Apr 7, 2016
    Posts:
    247
    @Kheremos
    That's exactly what I did, please read my second post too.
    I just tested the "IPointerClickHandler" version again and it still doesn't work in my project (Unity 2021.2.13f1). Not sure if I'm missing something else but at least "OnMouseDown" has been working just fine the last 10 months.