TextComponent
Text drawn in the world — a floating damage number, a sign, a score counter. (UI text uses the same component inside a canvas; see the UI guide.)
public class ScoreLabel : GameEntity
{
private TextComponent? m_Label;
private int m_Score;
protected override void OnCreate()
{
m_Label = GetComponent<TextComponent>();
}
protected override void OnUpdate(float ts)
{
m_Label?.SetInt(m_Score);
}
}Text
The string to display. Read and write.
m_Label.Text = "Game Over";SetInt(int value)
Shows an integer without allocating anything. Use this, not Text = $"{score}", for anything that updates every frame.
m_Label.SetInt(m_Score);SetFloat(float value, int digits = 1)
Shows a float with digits decimal places (clamped to 0–9), also allocation-free.
m_TimerLabel.SetFloat(m_TimeLeft, digits: 1);This is the single most common cause of stutter on phones
Text = $"{m_Score}" builds a fresh string every time it runs. One score counter at 60 fps is 60 short-lived strings a second, and the garbage collection that follows is a visible hitch on a Mono/AOT device.
SetInt/SetFloat pass the number across and let the engine format it, so the C# side allocates nothing at all. Same for SetFloat versus $"{time:0.0}".
Text = "Game Over" is fine — a literal allocates nothing, and a one-off assignment when state changes is not a per-frame path. It's the interpolation in OnUpdate that costs you. See Performance.
LocalizationKey
A key from your localization catalogs, like "menu.play". When it's non-empty, Text is re-resolved from the current language every frame — so a language switch updates the label with no code and no subscription.
m_Label.LocalizationKey = "menu.play";Set it to an empty string to go back to plain, untranslated text. See Loc and the Localization guide.
Color
A Vector4 of red, green, blue and alpha, each 0 to 1. Read and write.
m_Label.Color = new Vector4(1, 0.2f, 0.2f, 1); // redKerning
Extra space between characters. Read and write.
LineSpacing
Space between lines. Read and write.
See also
- Loc — translating text a script builds itself
- UI — canvases, buttons, and HUD layout
- Performance — why
SetIntexists