Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to reference TMPRO button

Discussion in 'UGUI & TextMesh Pro' started by yellsellsg, Mar 2, 2020.

  1. yellsellsg

    yellsellsg

    Joined:
    Jan 21, 2020
    Posts:
    9
    Well this isn't clear to me
    Looked high & Low

    How does one reference a "Button - TextMeshPro"

    I know

    "Text - TextMeshPro" is:
    .GetComponent<TMPro.TextMeshProUGUI>()

    and even
    a "dropdown - TextMeshPro" is
    .GetComponent<TMPro.TMP_Dropdown>()

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

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    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.
     
    Swagrim, gabrimo, Ryiah and 4 others like this.
  3. DevViktoria

    DevViktoria

    Joined:
    Apr 6, 2021
    Posts:
    94
    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.
     
    mistahRay and Nelmarticus like this.
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
  5. unity_R8LJg19THL7GBw

    unity_R8LJg19THL7GBw

    Joined:
    Sep 18, 2022
    Posts:
    1
    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
     
  6. Jonathanhong1999

    Jonathanhong1999

    Joined:
    Sep 26, 2020
    Posts:
    1
    thx
     
  7. JaredBGreat

    JaredBGreat

    Joined:
    Oct 28, 2017
    Posts:
    15
    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.
     
  8. bwoodwriter

    bwoodwriter

    Joined:
    Dec 27, 2022
    Posts:
    1
    Did you ever get this to work? I'm stuck with the same issue.
     
  9. IanceIot

    IanceIot

    Joined:
    Dec 30, 2023
    Posts:
    1
    Because "Button text Mesh pro" means a button with text type TMP. Thus its actually a button.
     
  10. JaredBGreat

    JaredBGreat

    Joined:
    Oct 28, 2017
    Posts:
    15
    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.
     
  11. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,274
    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.

    tmpb.png tmpb2.png
     
    spiney199 likes this.
  12. unity_6666179C4C5D5A378C06

    unity_6666179C4C5D5A378C06

    Joined:
    Oct 16, 2023
    Posts:
    1
    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:
     

    Attached Files: