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. Dismiss Notice

TextMesh Pro TryAddCharacters Null Pointer Exception

Discussion in 'UGUI & TextMesh Pro' started by eliashodel, Mar 19, 2020.

  1. eliashodel

    eliashodel

    Joined:
    Jan 25, 2019
    Posts:
    18
    Hi

    I'm trying to populate a FontAsset with all the Glyphs On Runtime.
    My thought was to do this with the
    TryAddCharacters 
    function. However I get a NullPointerException:
    Code (CSharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. TMPro.TMP_FontAsset.TryAddCharacters (System.String characters, System.String& missingCharacters) (at <2687edf00632410dad9a33972d48cfa4>:0)
    3. AddGlyphsDynamic.Start () (at Assets/Scripts/AddGlyphsDynamic.cs:25)
    My Code:
    Code (csharp):
    1. public TMP_FontAsset fontAsset;
    2.     public string glyphs;
    3.     // Start is called before the first frame update
    4.     void Start()
    5.     {
    6.         string notChars;
    7.         if (fontAsset.TryAddCharacters(glyphs, out notChars))    //<----- THIS IS LINE 25
    8.         {
    9.             Debug.Log(notChars);
    10.         }
    11.  
    12.     }

    My Font Asset Configuration:
    Screenshot_20200319_150000.png
    What am I missing?

    Maybe a short explanation why I want to do this:
    I have some textcontent i pull from a CMS, so i know on startup what the text is, and that it's not gonna change. Since there is also Chinese Text i don't want to load all existing glyphs but only the one i need later.
     
  2. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    6,588
    What version of the TMP package are you using?

    Do you have a list of the glyphs you are trying to add? It should not matter but just in case.
     
  3. eliashodel

    eliashodel

    Joined:
    Jan 25, 2019
    Posts:
    18
    Hi
    I'm on Unity 2019.2.21 using the TextMeshPro 2.0.1 Package
     
  4. eliashodel

    eliashodel

    Joined:
    Jan 25, 2019
    Posts:
    18
    Sorry it seems i cant post the chinese glyphs because its marked as spam...
    These are the unicodes instead:
    \u4f5c\u70ba\u6f22\u8a9e\u65cf\u5404\u7a2e\u6f22\u8a9e\u8a9e\u8a00
     
  5. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    6,588
    Please test using version 2.1.0-preview.8 of the TMP package.

    Since it is 2:15 AM for me, I will test the TryAddCharacters() using the unicodes you provided tomorrow and provide feedback once I have more information.
     
    eliashodel likes this.
  6. eliashodel

    eliashodel

    Joined:
    Jan 25, 2019
    Posts:
    18
    i tested with the preview package and i still get a NullPointerException:

    Code (CSharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. TMPro.TMP_FontAsset.TryAddCharacters (System.String characters, System.String& missingCharacters, System.Boolean includeFontFeatures) (at Library/PackageCache/com.unity.textmeshpro@2.1.0-preview.8/Scripts/Runtime/TMP_FontAsset.cs:1607)
    3. AddGlyphsDynamic.Start () (at Assets/Scripts/AddGlyphsDynamic.cs:15)
    I tested different fonts (latin and chinese) and different glyphs ant i always get this error...
    When i add the Chinese Glyphs not with the script, but by just enter them in the textfield form the TMP Component it workes...
     
  7. eliashodel

    eliashodel

    Joined:
    Jan 25, 2019
    Posts:
    18
    i was able to fix it, i had to read the fontasset first, otherwise the CharacterLookupDictionary is null.
    Code (CSharp):
    1.  
    2. public TMP_FontAsset fontAsset;
    3. public string glyphs;
    4. void TestAddCharacter()
    5. {
    6.         fontAsset.ReadFontAssetDefinition();
    7.         string notChars;
    8.         if (fontAsset.TryAddCharacters(glyphs, out notChars))
    9.         {
    10.             Debug.Log(notChars);
    11.         }
    12. }
     
  8. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    6,588
    Please test with Preview 13 to see if you still get the same behavior.

    You should not have to call ReadFontAssetDefinitions(). When does your code execute? In Awake?

    Can you provide me with a full work test project / scene so I can take a closer look to make sure I didn't overlook anything and that everything is working as expected?