Search Unity

Bug NullReferenceException after loading from AssetBundle

Discussion in 'UGUI & TextMesh Pro' started by NiklasP_, Dec 21, 2023.

  1. NiklasP_

    NiklasP_

    Joined:
    Jul 6, 2020
    Posts:
    8
    Hey,

    We're currently having an issue with loading TMP texts from an AssetBundle (AssetBundle.LoadFromFile()). We have a modding system, where players can create own models which can contain texts. In the editor, everything works fine, but in the build version, the texts are not visible and the log is spammed with this exception:

    NullReferenceException: Object reference not set to an instance of an object
    at TMPro.MaterialReference..ctor (System.Int32 index, TMPro.TMP_FontAsset fontAsset, TMPro.TMP_SpriteAsset spriteAsset, UnityEngine.Material material, System.Single padding) [0x0002b] in <c4628e9277b84292b2b249922d447608>:0
    at TMPro.TextMeshPro.SetArraySizes (TMPro.TMP_Text+TextProcessingElement[] textProcessingArray) [0x00083] in <c4628e9277b84292b2b249922d447608>:0
    at TMPro.TMP_Text.ParseInputText () [0x0004c] in <c4628e9277b84292b2b249922d447608>:0
    at TMPro.TextMeshPro_OnPreRenderObject () [0x0007e] in <c4628e9277b84292b2b249922d447608>:0
    at TMPro.TextMeshPro.Rebuild (UnityEngine.UI.CanvasUpdate update) [0x00035] in <c4628e9277b84292b2b249922d447608>:0
    at TMPro.TMP_UpdateManager.DoRebuilds () [0x00083] in <c4628e9277b84292b2b249922d447608>:0
    at (wrapper delegate-invoke) <Module>.invoke_void()
    at UnityEngine.Canvas.SendWillRenderCanvases () [0x0000a] in <363338bef28f4e51a67d97c913e2fc6d>:0


    This seems to be related with the font. We offer official fonts, which are already in the project and are replaced after importing the mod with the ones from the project, to save resources:

    Code (CSharp):
    1. var tmpTexts = mod.GameObject.GetComponentsInChildren<TMP_Text>(true);
    2. foreach (var tmpText in tmpTexts) {
    3.      var font = fonts.FirstOrDefault(font => font.name == tmpText.font.name);
    4.      if (font != null) {
    5.          tmpText.font = font;
    6.      }
    7. }
    These work fine, but if it is a custom font and it is not replaced with one from the project, the exception from above occurs in the build version and the text is not visible. Has anyone an idea, how we can fix that or is that a TMP bug?