1
\$\begingroup\$

If I select some GameObject in the Unity, e.g. recently added TextMeshProUGUI inside the Canvas, where can I find out the type of selected game object?

In Inspector window, same as in Properties window (shown using context menu on the game object in Hierarcy window), I can see the name, tag, layout, etc. But where is the type?

I need to know the type to reference the game object in the code:

using TMPro;
using UnityEngine;

public class Foo: MonoBehaviour
{
    public TextMeshProUGUI bar;
    ...
    public SomeMethod() => bar.text = "bla";
}

I've found this topic which is rather confusing than helpful. Sure, all of game objects are inherited from GameObject, but to access bar.text I will need to know its type anyway.

From other point of view, I can look for components in Inspector and always use bar.GetComponent<XYZ>(), but similarly, XYZ is not shown in Inspector, I can see "TextMeshPro - Text (UI)" caption, which is not really TMPro.TextMeshProUGUI type I need to use in the code. So I have come to another question, how to get component type... Great isn't it?

I am likely missing something obvious. Am I the first asking this question?

\$\endgroup\$
2
  • \$\begingroup\$ Your link explains it relatively well, maybe you haven't fully figured out what GameObjects and what components are. GameObjects are always just GameObjects, you can look at them as simply a bucket holding components of different types (like TextMeshPro ones, or MonoBehaviours that you write). \$\endgroup\$
    – Nikaas
    Feb 8, 2022 at 7:45
  • \$\begingroup\$ After reading answers, I figured out I should be looking for a component type, rather than game object type (was my confusion). @Nikaas, I am looking for a fastest way to do following: select object in scene, find the underlying type to reference its component, which property I am going to use (e.g. to set text or some other property, which may only exists for that specific component). \$\endgroup\$
    – Sinatr
    Feb 8, 2022 at 18:25

2 Answers 2

1
\$\begingroup\$

Note that a TextMeshProUGUI is a Component, not a GameObject. When you add a TextMeshProUGUI component it adds it to a new GameObject by default, which can be confusing. To make this more confusing, if you drag a GameObject into a Component slot, it will look for Components that match and add those directly.

If you have dragged the GameObject that contains the TextMeshProUGUI component in bar in the Editor, then you should just be able to set it with:

bar.SetText("bla");

Also note that you should use the SetText() method with the text mesh component instead of directly setting the text property.

\$\endgroup\$
4
  • \$\begingroup\$ I have expected TextMeshProUGUI to be an inherited from GameObject type. Thanks for clarifying, it was really helpful. Do you refer to text performance in the last paragraph or do you mean something else? I would appreciate a link. \$\endgroup\$
    – Sinatr
    Feb 8, 2022 at 18:15
  • \$\begingroup\$ Looks like it is for a special case. The SetText() documentation says "You may wish to use this function instead of TextMeshPro.text if you need to concatenate a string with values and trying to avoid unnecessary garbage collection.". digitalnativestudios.com/textmeshpro/docs/ScriptReference/… I always thought it was for historical reasons as setting Unity's Text.text property was the way to do this before TextMeshPro. \$\endgroup\$ Feb 8, 2022 at 18:52
  • 1
    \$\begingroup\$ This may have changed after Unity acquisition as current Unity docs says they are the same: "This function is the same as using the text property to set the text." docs.unity3d.com/Packages/[email protected]/api/… \$\endgroup\$ Feb 8, 2022 at 19:05
  • \$\begingroup\$ @GuneyOzsan Ah thanks, have to admit I haven't used TMP for a bit, so maybe this isn't such an issue now. \$\endgroup\$ Feb 8, 2022 at 23:49
1
\$\begingroup\$

Since you can add multiple components on a GameObject, there is no defined type for a GameObject (i.e. its type is already a GameObject). From there you can only try GetComponent<>() (or GetComponent(s)InChildren<>() for more complex objects) and see if there is one.

var textComponent = GetComponent<TextMeshProUGUI>();
if (textComponent != null) { // do something }

Or, you can use a serialized field and assign it manually from Unity editor, or automatically during Awake().

About your question on the naming of TextMeshPro components, they use a specific attribute to display a different name in Editor (which I believe is an anti-pattern for Unity). If you check the TextMeshProUGUI source code you can see it there:

...
[AddComponentMenu("UI/TextMeshPro - Text (UI)", 11)]
...
public partial class TextMeshProUGUI : TMP_Text, ILayoutElement
{
    ...
{

` For finding such type names conveniently, you can do a text search across the solution for the name you see in Unity editor (e.g. UI/TextMeshPro - Text (UI)) and see type names (e.g. TextMeshProUGUI) in source code instead of struggling around the documentation or web.

\$\endgroup\$
2
  • \$\begingroup\$ Aye, it seems I need to find the component type (as per @Chriss-Trott answer). Searching in solution is a good hint thanks, I was hoping for a more "visual" way, similar to how visual studio property window typically shows the selected object type in various designers (wpf, winforms, ...). The official website didn't even find the said component when pressing (?) button in inspector. Should I've looked here? \$\endgroup\$
    – Sinatr
    Feb 8, 2022 at 18:22
  • 1
    \$\begingroup\$ @Sinatr As I said this is quite an anti-pattern that I never saw any other developer using different names for Editor display and the class. You may never need to do a text search in solution ever again:) If I don't use TextMeshPro for a while I always struggle to guess which component was what. No, you shouldn't have looked at the docs because looking there assume you already know the type name, which is what you are looking for. It is a design fault from my perspective. \$\endgroup\$ Feb 8, 2022 at 19:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .