Search Unity

Resolved Change TextMeshPro font by script

Discussion in 'UGUI & TextMesh Pro' started by ARunitystudi, Aug 22, 2021.

  1. ARunitystudi

    ARunitystudi

    Joined:
    Apr 25, 2021
    Posts:
    21
    Hello, I want to change the font of a TextMeshPro text by script.
    Therefore, I placed a font-file (Baloo.ttf) in the Resources folder to access it from my script.
    I am loading and assigning my font this way:

    Code (CSharp):
    1. Font font = Resources.Load<Font>("Baloo");
    2. TMP_FontAsset asset = TMP_FontAsset.CreateFontAsset(font);
    3. textMeshPro.font = asset; // `textMeshPro` is created this way: TextMeshPro textMeshPro = gameObject.AddComponent<TextMeshPro>()
    But this way an error is thrown in line 2 or 3:
    Code (CSharp):
    1. Error Unity NullReferenceException: Object reference not set to an instance of an object
    How can I change the font the right way?
     
    Last edited: Aug 22, 2021
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    In the above code, you are trying to assign this newly created font asset to a “textMeshPro” object. Does that object point to a valid reference to a TMP object?
     
  3. ARunitystudi

    ARunitystudi

    Joined:
    Apr 25, 2021
    Posts:
    21
    I am adding the TextMeshPro component as shown above to my gameobject, which is a valid object:
    Code (CSharp):
    1. TextMeshPro textMeshPro = gameObject.AddComponent<TextMeshPro>();
    Then I use it for setting my text and its color, which are both displayed correctly.
    Only the font is not changing. When I add the lines 2 and 3 from above, the error from above is thrown.
    Without them everything is fine.
     
  4. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    I would suggest stepping through the code to see if the Resource.Load does correct load the font object that you are trying to use to create the font asset from.
     
  5. ARunitystudi

    ARunitystudi

    Joined:
    Apr 25, 2021
    Posts:
    21
    Thanks, the font is loaded correctly, at least the object is not null, and stores the correct font. But I still get the same error:
    Code (CSharp):
    1. 2021.08.23 19:06:46.152 30659 30727 Error Unity NullReferenceException: Object reference not set to an instance of an object
    2. 2021.08.23 19:06:46.152 30659 30727 Error Unity   at TMPro.TMP_FontAsset.CreateFontAsset (UnityEngine.Font font, System.Int32 samplingPointSize, System.Int32 atlasPadding, UnityEngine.TextCore.LowLevel.GlyphRenderMode renderMode, System.Int32 atlasWidth, System.Int32 atlasHeight, TMPro.AtlasPopulationMode atlasPopulationMode, System.Boolean enableMultiAtlasSupport) [0x00016] in C:\Users\AR-Study\AR-App\Library\PackageCache\com.unity.textmeshpro@3.0.6\Scripts\Runtime\TMP_FontAsset.cs:456
    3. 2021.08.23 19:06:46.152 30659 30727 Error Unity   at TMPro.TMP_FontAsset.CreateFontAsset (UnityEngine.Font font) [0x00001] in C:\Users\AR-Study\AR-App\Library\PackageCache\com.unity.textmeshpro@3.0.6\Scripts\Runtime\TMP_FontAsset.cs:434
     
  6. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    On line 2, you are creating a TMP_FontAsset, when you step through the code, does that result in a valid font asset or a null object? Do you get a warning in the console?

    Make sure this Baloo font is set to dynamic.

    If none of the above point to anything, please provide a simple repro project of the above and I can take a closer.
     
  7. ARunitystudi

    ARunitystudi

    Joined:
    Apr 25, 2021
    Posts:
    21
    Yes, it is a vaild asset, not a null object. I did not get any warnings beside this error.
    I also tried to create a TextMeshProUGUI object through the ui and change its font the same way in my script. This works perfectly with a TextMeshProUGUI object.
    But in my case, I need to change the font of text, that I created via script and has the type TextMeshPro, not TextMeshProUGUI. Also, the class I am creating and changing the font in is not a MonoBehaviour in case this is relevant. Do I maybe have to change something in the creation or assignment of the asset in this case?
     
  8. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Can you submit a bug report with the included project?

    This would be the easiest way for me to look at your implementation to figure out why you are getting those errors.

    If you do submit a bug report, please provide the Case # once you have it from Unity.
     
  9. ARunitystudi

    ARunitystudi

    Joined:
    Apr 25, 2021
    Posts:
    21
    While I was trying to create a minimal example, I noticed that this error only occurs, when I have all three lines in the Awake method. As I do not want to change the font afterwards, and the textmesh is also created there, I thought that this is the right place to set it. Now, I am only creating the asset there, but I assign it in the Update method. This works fine. Thanks a lot for your help.
     
  10. MoonPlayMedia

    MoonPlayMedia

    Joined:
    Nov 28, 2021
    Posts:
    5
    Hallo I like to ask how to resolve this. I have TMPro and i always used it but i never have this error. After this error i used
    textMeshPro FontCreator but i still get this error. Iam using FontAsset but it still not work.

    Please assign a Font Asset to this Coins_Text (TMP) gameobject.
    UnityEngine.Debug:LogWarning (object,UnityEngine.Object)
    TMPro.TextMeshProUGUI:OnPreRenderCanvas () (at Library/PackageCache/com.unity.textmeshpro@3.0.6/Scripts/Runtime/TMPro_UGUI_Private.cs:1631)
    TMPro.TextMeshProUGUI:Rebuild (UnityEngine.UI.CanvasUpdate) (at Library/PackageCache/com.unity.textmeshpro@3.0.6/Scripts/Runtime/TextMeshProUGUI.cs:216)
    UnityEngine.Canvas:SendWillRenderCanvases ()