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)?

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.

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.

If anyone is still searching for this and wants to do it in code just add this function anywhere you can call it:

        public static void AddEvent(EventTrigger evtT , EventTriggerType type, UnityAction<BaseEventData> del)
        {
            var entry = new EventTrigger.Entry
            {
                eventID = type
            };
            entry.callback.AddListener(del);
            evtT.triggers.Add(entry);
        }

Usage:

        public void Start()
        {
            var evTrigger = YourTextMehProLabel.AddComponent<EventTrigger>();
            AddEvent(evTrigger, EventTriggerType.PointerClick, OnPointerClick);
        }

Function called in this event (can be what ever you want as long as the signature is respected):

        public void OnPointerClick(BaseEventData evt)
        {
            Debug.Log("Pointer clicked");
        }

Works fine in the latest LTS of v6

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.