How do you register a click on a TMP text?

Hello everyone,

I’ve got a regular TextMeshPro text GameObject (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 (Unity 2021.1.6f1)?

Hello, I’m assuming you’ve moved past this, but in case someone else stumbles upon this search result (which is high on the list) one way to do this is by adding an Event Trigger component to your text object (via the GUI). Then click [Add New Event Type] and select PointerClick. Then you just populate the trigger conditions and method(s) as usual.

You can add a Button component and remove the Image if you simply want an invisible clickable area with events. This will use the UI’s rectTransform to determine the boundaries for click events.

The only way that (currently) works I’ve found to be OnMouseDown() but only if you add a collider (BoxCollider is probably the simplest, no need to tick “Is Trigger”) to the GameObject.