Change TextMeshPro font by script

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:

Font font = Resources.Load<Font>("Baloo");
TMP_FontAsset asset = TMP_FontAsset.CreateFontAsset(font);
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:

Error Unity NullReferenceException: Object reference not set to an instance of an object

How can I change the font the right way?

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?

I am adding the TextMeshPro component as shown above to my gameobject, which is a valid object:

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.

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.

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:

2021.08.23 19:06:46.152 30659 30727 Error Unity NullReferenceException: Object reference not set to an instance of an object
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
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

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.

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?

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.

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.

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 ()