How to reference TMPRO button

Well this isn't clear to me
Looked high & Low

How does one reference a "Button - TextMeshPro"

I know

"Text - TextMeshPro" is:
.GetComponent()

and even
a "dropdown - TextMeshPro" is
.GetComponent()

but what is the equivalent for the button please. Documentation on this i cannot find!

The list of standard objects you can create from that menu isn't one-to-one with the actual classes. I believe a "Button - TextMeshPro" is just a standard UnityEngine.UI.Button with a child that has a TMPro.TextMeshProUGUI component instead of a UnityEngine.UI.Text component.

7 Likes

I checked the type of that kind of button with object.GetType() and it was UnityEngine.GameObject. So I guess you should reference it with GameObject.

2 Likes

@DevViktoria you may want to review the relationship between GameObjects and Components

The Button - TextMeshPro object can be referenced like a normal button,

using UnityEngine.UI

public Button button;

I'm not sure why it doesn't use TMPro, but I just tested this and it works

2 Likes


thx

Unfortunately that isn't true, I just tried it, and it doesn't work. It doesn't work as a GameObject, either. Its doesn't see that it is recognized as anything I can think to try.


Did you ever get this to work? I'm stuck with the same issue.


Because "Button text Mesh pro" means a button with text type TMP. Thus its actually a button.


I found a different way to do it for my use case -- by putting a script on the button and accessing through that; I'm not sure if that will work in your use case, but that is what worked for me.

It's not complicated. When you choose to add a "Button - TextMeshPro", you get multiple objects with multiple components. It's kind of like a Prefab pre-made recipe to combine text and clicky features in one.

The parent object inserted has the Button class component. Same Button used throughout standard Unity GUI. If you want to add click events or handle click events, or enable or disable clicking, refer to this Button component.

It also has an Image and Canvas Renderer, so it shows up on the canvas.

The child object inserted has the TextMeshProUGUI class component. This is for the text that will appear in the button on the canvas. If you want to set the text that appears, then refer to this TextMeshProUGUI component.

9571492--1354459--tmpb.jpg 9571492--1354462--tmpb2.png

1 Like

I figured out a fix. For whatever reason, there's ambiguity when referencing Button as a component. To fix it, I just directly referenced UnityEngine.UI.Button when defining the variable instead of putting it at the top of the code:

9647759--1372307--Screenshot 2024-02-16 100014.png