Exception thrown from the material property of orphaned TMP_SubMeshUI scripts

I have found that accessing the material property of a TMP_SubMeshUI script can cause an exception in some situations.

I can consistently replicate this by:

  1. Adding a TextMeshProUGUI script in a scene under a canvas
  2. Make it generate a TMP_SubMeshUI component for you by using a character that the font asset doesn’t have.
  3. Use a script to accessing the material property of the TMP_SubMeshUI during Start().

The exception (shown below) was triggered by a script CrashExample acessing the material during Start(). Accessing it from Update() will result in the exception firing once, but it becomes valid on later frames.

If you end up with an incorrectly setup TMP_SubMeshUI component in the scene the material property will always fire this exception. You can force this scenario by just manually adding a TMP_SubMeshUI component to a scene. This isn’t normal usage, but I have hit into this by deleting a TextMeshProUGUI component and neglecting to check and delete the SubMesh object created under it.

I am using Unity 2018.4.18f1 with TextMeshPro 1.4.1.

ArgumentNullException: Value cannot be null.
Parameter name: source
UnityEngine.Material…ctor (UnityEngine.Material source) (at C:/buildslave/unity/build/Runtime/Export/Shader.bindings.cs:109)
TMPro.TMP_SubMeshUI.CreateMaterialInstance (UnityEngine.Material source) (at Library/PackageCache/com.unity.textmeshpro@1.4.1/Scripts/Runtime/TMP_SubMeshUI.cs:758)
TMPro.TMP_SubMeshUI.GetMaterial (UnityEngine.Material mat) (at Library/PackageCache/com.unity.textmeshpro@1.4.1/Scripts/Runtime/TMP_SubMeshUI.cs:737)
TMPro.TMP_SubMeshUI.get_material () (at Library/PackageCache/com.unity.textmeshpro@1.4.1/Scripts/Runtime/TMP_SubMeshUI.cs:59)
CrashExample.Start () (at Assets/CrashExample.cs:12)

Please test using Preview 14 as Sub Mesh objects are no longer serialized.

As such, they are created as needed and no longer saved in the scene where potential of these might be required. Basically, you essentially no longer need to pay attention to them.