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:
Implement "IPointerClickHandler" in "ClickText", with "OnPointerClick(PointerEventData eventData)" - didn't work.
Add a "PointerClicker" event trigger for the GO (through the editor) and set it to call "ClickText.onClick()" function - didn't work.
"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)?
Answer by Kheremos · Dec 04, 2021 at 09:05 PM
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.
As mentioned in the question, the "Event Trigger" route didn't work - I just tested it again and it still doesn't. Yes, there's an active EventSystem in the scene but I'm not sure what else I need. I'm now using "OnMouseDown" with a BoxCollider. Btw, I also posted a thread in the forums, which might be the better place if anyone wants to "investigate" things.
I'm sorry, I promise I read the question, but only the first time I came to this page. After that I went and tinkered, and your #2 (that didn't work for you) worked for me. Was this happening to you even in an empty project? I'll check the Unity forums thread to see if there are further updates. Also, I said put it on the text object, you said you put it on the GO, that may make a difference?
I replied in the forum thread.
But yes, I added everything (including the mesh collider) directly to the 3D text (= its GO), not a parent or child. No, I haven't tested it in an empty project yet but I'm not sure why it would work there and not in my current project anyway.
Answer by SimRuJ · Dec 06, 2021 at 09:45 AM
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.
Answer by spaceman8 · Mar 01 at 10:55 AM
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.
A (TMP) button requires a canvas, a stand-alone 3D TMP text doesn't.
Edit: Sorry, misunderstood you. I just added a "Button" component to the text directly (which automatically set the "target graphic" to the text) and set its "OnClick" but it doesn't trigger either.
Oh yes, sorry I should have read your question more closely. Without a canvas I believe you will need to setup your own ray caster and collider test and do this logic yourself.