Your Cart
Loading
unity change text color in script unity textmeshpro change text color

Unity Change Text Color In Script | Unity Tutorial

Unity Change Text Color In Script | Unity Tutorial


In this Unity tutorial, you'll learn how to dynamically change text color using scripts. We’ll cover how to use both Unity’s legacy Text component and TextMesh Pro to update text color by specifying color codes. Follow along to master adjusting text colors at runtime and enhance your UI elements with vibrant, customizable colors!



WATCH FULL TUTORIAL ON YOUTUBE



CHECK OUR PROJECTS


SUBSCRIBE FOR LATEST OFFERS



=========================================================


Unity Projects - ON SALE!!!


1. Water Sort Puzzle 9000 Levels

https://payhip.com/b/cjDb8


2. Ball Sort Puzzle 9000 Levels

https://payhip.com/b/IbtoD


3. Sling Shot

https://payhip.com/b/PjLDH



=========================================================



Legacy Script:


using UnityEngine;

using UnityEngine.UI; // Include this to access the UI components


public class TextColorUpdater : MonoBehaviour

{

  public Text textComponent; // Reference to the Text component

  public string currentColorCode = "#FFFFFF"; // Default to white

  public string newColorCode = "#FF5733"; // Default to a specific color


  void Start()

  {

    if (textComponent != null)

    {

      // Set the initial color from the currentColorCode

      textComponent.color = ColorFromHex(currentColorCode);

    }

  }


  public void ChangeTextColor()

  {

    if (textComponent != null)

    {

      textComponent.color = ColorFromHex(newColorCode);

    }

  }


  Color ColorFromHex(string hex)

  {

    // Remove the '#' if it exists

    if (hex.StartsWith("#"))

    {

      hex = hex.Substring(1);

    }


    // Parse the hex color code

    byte r = byte.Parse(hex.Substring(0, 2), System.Globalization.NumberStyles.HexNumber);

    byte g = byte.Parse(hex.Substring(2, 2), System.Globalization.NumberStyles.HexNumber);

    byte b = byte.Parse(hex.Substring(4, 2), System.Globalization.NumberStyles.HexNumber);

    return new Color32(r, g, b, 255);

  }

}



TMP Script:


using UnityEngine;

using TMPro; // Include this to access TextMeshPro components


public class TMPTextColorChanger : MonoBehaviour

{

  public TMP_Text textMeshProComponent; // Reference to the TextMeshPro component

  public string currentColorCode = "#FFFFFF"; // Default to white

  public string newColorCode = "#33FF57"; // Default to a specific color


  void Start()

  {

    if (textMeshProComponent != null)

    {

      // Set the initial color from the currentColorCode

      textMeshProComponent.color = ColorFromHex(currentColorCode);

    }

  }


  public void ChangeTextColor()

  {

    if (textMeshProComponent != null)

    {

      textMeshProComponent.color = ColorFromHex(newColorCode);

    }

  }


  Color ColorFromHex(string hex)

  {

    // Remove the '#' if it exists

    if (hex.StartsWith("#"))

    {

      hex = hex.Substring(1);

    }


    // Parse the hex color code

    byte r = byte.Parse(hex.Substring(0, 2), System.Globalization.NumberStyles.HexNumber);

    byte g = byte.Parse(hex.Substring(2, 2), System.Globalization.NumberStyles.HexNumber);

    byte b = byte.Parse(hex.Substring(4, 2), System.Globalization.NumberStyles.HexNumber);

    return new Color32(r, g, b, 255);

  }

}





DOWNLOAD CODESTER FILES