Using Multiple TMP_Text Components in Unity: A Complete Guide

Published: 21 March 2025
on channel: vlogize
26
like

Learn how to utilize multiple `TMP_Text` components in Unity effectively by creating empty GameObjects and calling them in your code!
---
This video is based on the question https://stackoverflow.com/q/74184287/ asked by the user 'Liora' ( https://stackoverflow.com/u/3481335/ ) and on the answer https://stackoverflow.com/a/74184393/ provided by the user 'Igor Belikov' ( https://stackoverflow.com/u/11368147/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.

Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: Can I use more than one TMP_Text or TextMeshPro in Unity?

Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l...
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license.

If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
Using Multiple TMP_Text Components in Unity: A Complete Guide

Unity, a popular game engine, utilizes TextMeshPro (TMP) for enhanced text rendering. Many developers have encountered a common question: Can I use more than one TMP_Text or TextMeshPro in Unity? This question often arises when developers want to display multiple pieces of text simultaneously but find that their text only appears on the first TMP_Text instance they created.

In this guide, we will dive into the solution for this challenge. Let's explore how you can effectively use multiple TMP_Text instances and display various results in your Unity project.

The Problem: Displaying Multiple Text Results

Imagine you're developing a game or an application in Unity, and you want to show two different results on the screen. You've set up your first TMP_Text, but as you try to add more, you realize that only the text assigned to the first one is displaying. This can be frustrating, especially if you need to present various information or results.

The Solution: Creating Multiple TMP_Text Components

Setting Up Multiple TextMeshPro Instances

Fortunately, Unity allows you to create multiple instances of TMP_Text. Here’s how you can do it:

Create Empty Game Objects:

In the Unity Hierarchy, right-click and select Create Empty.

Name this GameObject something meaningful (e.g., "ResultText1", "ResultText2").

Repeat this step for however many text outputs you need.

Assign TMP_Text Components:

With your new empty GameObject selected, click Add Component in the Inspector panel.

Search for TMP_Text and add it to the GameObject.

Customize the TMP_Text settings as required (font size, color, alignment, etc.).

Accessing the TMP_Text Instances in Code

To manipulate these TMP_Text components effectively, you need to access them in your script. Here’s how to set that up:

Create References in Your Script:

Add using TMPro; at the start of your script to include the necessary namespace.

Declare the TMP_Text fields that will hold references to your TextMeshPro components:

[[See Video to Reveal this Text or Code Snippet]]

Assign the Components in the Inspector:

Go back to the Unity editor.

Click on the GameObject that has your script and navigate to the Inspector.

You should see the resultText1 and resultText2 fields exposed. Drag and drop your TMP_Text GameObjects into these fields.

Updating the Text Content

Finally, you can update the content of your TMP_Text components in your code. Here’s a sample code snippet:

[[See Video to Reveal this Text or Code Snippet]]

With the above code, you can dynamically change the displayed text based on game events, user inputs, or other conditions.

Conclusion

Using multiple TMP_Text components in Unity is not only possible, but it's also straightforward when you know the right steps. By creating empty GameObjects, assigning the TMP_Text components, and accessing them via code, you can easily display various text outputs in your game or app. This approach adds more flexibility to your user interface and enhances the overall user experience. Happy coding!


On this page of the site you can watch the video online Using Multiple TMP_Text Components in Unity: A Complete Guide with a duration of hours minute second in good quality, which was uploaded by the user vlogize 21 March 2025, share the link with friends and acquaintances, this video has already been watched 26 times on youtube and it was liked by like viewers. Enjoy your viewing!