Search Unity

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

Unity UI Frustrating amount of time spent on trying to use GetComponent<Text>()

Discussion in 'UGUI & TextMesh Pro' started by lonn99, Nov 14, 2023.

Thread Status:
Not open for further replies.
  1. lonn99

    lonn99

    Joined:
    Sep 1, 2023
    Posts:
    6
    I was trying to set a text field to automatically update itself whenever a slider updated. Unity (currently) does not have any methods to update text through a method, which meant I needed to put it in a script.

    So I did that, I added the first UI text object I found (TextMeshPro) and tried to use GetComponent<Text>()

    Some of you will already see my mistake, I should be using GetComponent<TMP_Text>() instead.

    But I didn't know that, and it took me 2 hours of furiously searching other help posts, only finding what I needed when I had the thought that maybe I needed TextMeshPro. Except I also discovered there are multiple ways to reference some object known by TextMeshPro, and so I tried every one of them until I found TMP_Text.

    I am new to Unity, I make heavy use of the documentation when learning something new about the engine. If the new way to use Text is TextMeshPro, there should be ample warnings in the documentation on the legacy objects that this feature has been added, because tutorials and old help posts do not update with Unity.

    Please add information about this and other features like it, because it is not reflected in the documentation, and I can't even find TMP_Text when I search for it.
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,238
  3. NicK4rT

    NicK4rT

    Joined:
    May 21, 2022
    Posts:
    1
    lonn99 likes this.
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,238
    Not really my point, I was noting that documentation for every Unity package is separate to the core Unity documentation.

    TMP_Text can be found in the scripting API section. And it does come up via a google search.

    In any case this kind of newbie mistake isn't unique to uGUI/TMPro. It's something all newbies will make in one way or another. Then they learn, and don't make this mistake again. If they don't make it with this, it'll be something else.

    It could be in the docs but 95% of newbies would miss it anyway as 95% of newbies don't read the docs.
     
  5. lonn99

    lonn99

    Joined:
    Sep 1, 2023
    Posts:
    6
    Sorry, it's been a month, and I initially wrote this largely out of frustration, but I'll clarify why this bothered me enough to post.
    In particular, I was looking at this part of the scripting API documentation: https://docs.unity3d.com/2018.4/Documentation/ScriptReference/UI.Text.html

    The reason for this was because I was found through old Unity tutorials that the way to update text was through using this section. But there is no mention of TMP_Text or TextMeshPro which appear FIRST when you want to add text to a UI element. In order to use the component referred to by this document you need to specifically find the "Text" component under a legacy tab. Except I wanted to use TextMeshPro, because maybe I'd want some of the features it brings, and I figured it would be easy enough.

    So I assumed naturally I should use something like:
    Code (CSharp):
    1. GetComponent<TextMeshPro>()
    Because hey that's literally what it's called in the inspector. And it didn't work. So I searched, "TextMeshPro," I actually found that document you linked. It didn't mention TMP_Text. So, what am supposed to do?

    It took me stumbling into this: https://forum.unity.com/threads/access-textmeshpro-text-through-script.527992/ and this: https://forum.unity.com/threads/getcomponent-textmeshpro-text.1201804/ to realize my mistake.

    I didn't google TMP_Text as you said I could, because how was I supposed to know that's what the component was called if it appears as "TextMeshPro - Text (UI)" in the inspector, and after looking up "TextMeshPro" and finding no hint of the name of its text box?

    If Unity is going to hide legacy components to push you to new ones, or offer support for these new ways of displaying text or other features, I'd like a hint at least in the documentation, or at least it be searchable in the main site labeled "Unity Documentation?" I really didn't like how I had to scramble into official document into another and not find a whiff that it had a specific unexpected name so I could write one line of code. That was my complaint.
     
    Last edited: Dec 9, 2023
  6. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,238
    Right so you were doing old, out of date tutorials, and looking at the documentation for old, out of date Unity versions, and it's Unity's fault that you were lead astray?

    Maybe do some up to date tutorials and you won't have this issue.
     
  7. lonn99

    lonn99

    Joined:
    Sep 1, 2023
    Posts:
    6
    I'm not letting you passive-aggressively insult me instead of understanding the issue I brought up. My complaint is literally with the Unity documentation. What's your deal?

    Typically, I can do a search in the scripting API and find out what I need far faster than poring over video tutorials about the problem, or if I wanted to adjust other aspects of that specific component, it would've been nice to read about it as well. Do you really have so much of a problem with people wanting a warning or hint about newer systems/packages?
     
    Last edited: Dec 9, 2023
  8. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,238
    I mean the documentation for every official package is available throught the package manager:
    upload_2023-12-9_14-26-57.png

    It makes sense that if you are using a newer version of Unity, you are either:
    A: Are reading up to date documentation
    B: Or reading the documentation for the associated package

    Unity's documentation is pretty comprehensive. You just got a little lost because you're new to Unity. Every newbie will do this in some form or another, whether it be getting UI Toolkit confused with the older systems, of not knowing of TMPros vs the old legacy UI components. Oh well, lesson learnt. Now you won't make the same mistake.
     
  9. lonn99

    lonn99

    Joined:
    Sep 1, 2023
    Posts:
    6
    Amazing, you finally pointed to a resource that could've helped me with the issue I initially brought up. I would thank you, but a cursory glance at both https://docs.unity3d.com/Packages/com.unity.textmeshpro@3.0/manual/index.html, https://docs.unity3d.com/Packages/com.unity.ugui@1.0/manual/script-Text.html reveals not one mention of TMP_Text.

    It's almost as if the issue is that it's not mentioned in the resources that one would expect to describe its use.
     
  10. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,238
    Then report an issue with the documentation in the page. Ranting about it won't make any changes happen.
     
  11. UGUI 1.0 did NOT contain TextMeshPro. The inclusion of TextMeshPro happened in UGUI 2.0, fairly recently. You will never have old documentation containing new info which wasn't true for the version you're looking at. Please try to synchronize your version numbers, look at the same documentation version as the packages you're using otherwise you will have a really bad time with any kind of software that changes often. Unity does change often.
     
  12. lonn99

    lonn99

    Joined:
    Sep 1, 2023
    Posts:
    6
    Pardon my apparent mistake, and apparently my "ranting," I assumed this would be a good place to post this. Searching about people complaining about Unity's documentation reveals that people have complained about it in general. Yet they also say it is quite good, and I agree, it is good in general, and that's why I started this.

    But you sir, in particular have not been helpful. You could amend that by actually pointing to the way that one would go about "reporting the issue" to any "fellow newbies" such as myself who may or may not stumble into the forums looking for why their Text components don't work like they expect them to.

    I appreciate this actually, though I think the version of Unity I'm using doesn't have UGUI 2.0, I don't have an option to update it. But I opened the docs for TextMeshPro again and actually found what I was looking for, a reference to the TMPro namespace and TMP_Text! If only I knew how to navigate it before.
     
  13. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,238
    Scroll to the bottom of any documentation page. Not hard to miss.
     
  14. lonn99

    lonn99

    Joined:
    Sep 1, 2023
    Posts:
    6
    Thank you. For real this time.
     
  15. APSchmidt

    APSchmidt

    Joined:
    Oct 31, 2023
    Posts:
    248
    Don't use Get component<T>(), make the variable either "public" or "[SerializeField] private" and add the field in the Inspector.
    This goes for all components types. :)
     
  16. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,025
    Closing.
    1) You can find many excellent beginner tutorials at https://unity.com/learn
    2) Make sure any resources you are using for learning (docs, tutorials, etc) match the version of Unity you are using
    3) If you don't understand something ask a question rather than assuming the learning materials are the problem.
     
Thread Status:
Not open for further replies.