Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

TextMesh Pro - set fontStyle via script

Discussion in 'Scripting' started by Kasi2302, Apr 15, 2022.

  1. Kasi2302

    Kasi2302

    Joined:
    Sep 30, 2020
    Posts:
    25
    using TMPro;

    [SerializeField] private TMP_Text quadTermsLegalAge;

    quadTermsLegalAge.GetComponent<TMP_Text>().fontStyle = FontStyle.Bold;


    Error:

    Assets\Scripts\SceneMainController_Scene2.cs(193,76): error CS0266: Cannot implicitly convert type 'UnityEngine.FontStyle' to 'TMPro.FontStyles'. An explicit conversion exists (are you missing a cast?)

    Why is this not working ??? :(

    Unity Version: 2020.3
    TextMeshPro Version: 3.0.6
     
  2. glgamesforfun

    glgamesforfun

    Joined:
    Apr 13, 2021
    Posts:
    149
    You can do a explicit conversion with (FontStyles):
    Code (CSharp):
    1.         quadTermsLegalAge.GetComponent<TMP_Text>().fontStyle = (FontStyles)FontStyle.Bold;
    This works fine for me :)
     
  3. Kasi2302

    Kasi2302

    Joined:
    Sep 30, 2020
    Posts:
    25
    Thank you very very much. :)
     
    glgamesforfun likes this.
  4. NobleRobot

    NobleRobot

    Joined:
    Jan 14, 2016
    Posts:
    56
    UnityEngine.FontStyle is used by the old "Text Mesh" component. The correct type to use is TMPro.FontStyles, which is the Text Mesh Pro equivalent. These are both enums, and casting from one to the other will mostly work (FontStyle.Bold and FontStyles.Bold are both equal to "1", for example), but the values are not guaranteed to line up with each other in all cases for all time.

    So, your code would simply be (as long as you're using TMPro;):

    Code (csharp):
    1. quadTermsLegalAge.GetComponent<TMP_Text>().fontStyle = FontStyles.Bold;