Search Unity

TextMesh Pro Error: MissingReferenceException with Material

Discussion in 'UGUI & TextMesh Pro' started by ChronoWalker, May 29, 2020.

  1. ChronoWalker

    ChronoWalker

    Joined:
    Sep 24, 2017
    Posts:
    6
    I'm currently using TMPro with v2.0.1 and unity 2019.2.19f1, and each time after I run my game, console starts to complain with this message:
    ```
    MissingReferenceException: The object of type 'Material' has been destroyed but you are still trying to access it.
    Your script should either check if it is null or you should not destroy the object.
    TMPro.TextMeshProUGUI.GenerateTextMesh () (at Library/PackageCache/com.unity.textmeshpro@2.0.1/Scripts/Runtime/TMPro_UGUI_Private.cs:2094)
    TMPro.TextMeshProUGUI.OnPreRenderCanvas () (at Library/PackageCache/com.unity.textmeshpro@2.0.1/Scripts/Runtime/TMPro_UGUI_Private.cs:1656)
    TMPro.TextMeshProUGUI.Rebuild (UnityEngine.UI.CanvasUpdate update) (at Library/PackageCache/com.unity.textmeshpro@2.0.1/Scripts/Runtime/TextMeshProUGUI.cs:209)
    UnityEngine.UI.CanvasUpdateRegistry.PerformUpdate () (at C:/Program Files/Unity/Hub/Editor/2019.2.19f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/CanvasUpdateRegistry.cs:198)
    UnityEngine.Canvas:SendWillRenderCanvases() (at C:/buildslave/unity/build/Modules/UI/ScriptBindings/UICanvas.bindings.cs:72)
    ```
    I have read this post https://forum.unity.com/threads/get...t-of-type-material-has-been-destroyed.799671/, but my textmesh pro version is already the latest.
    Though it doesn't prevent me from starting the game, it becomes very annoying and concerning for me. Really appreciate if someone could help fix this!
     
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Version 2.0.1 is the latest verified version but it is not the latest version.

    I would suggest updating to the latest release of the TMP package which is 2.1.0-preview.13. This release is still in preview but will become the verified version for 2019.x.

    I do expect to release Preview 14 next week. Preview 14 is expected to be the last preview release.

    P.S. To see preview releases, you have to enable Show Preview Packages in the Advanced tab of the Package Manager Window as seen below.

    upload_2020-5-29_15-22-18.png
     
    Last edited: May 29, 2020
    kimyir2 and ChronoWalker like this.
  3. bcol

    bcol

    Joined:
    Jun 2, 2018
    Posts:
    1
    I've also encountered the same problem in 2019.3.12f (macOS) with TextMeshPro 2.0.1.
    change line 2094 in /com.unity.textmeshpro@2.0.1/Scripts/Runtime/TMPro_UGUI_Private.cs to

    Code (CSharp):
    1. if (m_currentMaterial != null && m_currentMaterial.HasProperty(ShaderUtilities.ID_GradientScale))
    seems like to fix the problem. I also find where the problem comes from in my project.

    I've created a font asset with custom character set, which do not contain the original ASCII characters. So if I set the text with both my custom characters and ASCII characters, a sub-mesh text (fall-back) will be created as a child game object. If I continue to change the text via script and the new text do not contain ASCII characters, the child sub-mesh should somehow be destroyed and the problem occurs when I quit game mode.
     
  4. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    I would suggest using the latest release of the TMP package which is version 2.1.0-preview.13 for Unity 2019.x. These issues should be resolved in those releases.

    Also make sure to update the TMP Essential Resources has these have been updated.
     
  5. ChronoWalker

    ChronoWalker

    Joined:
    Sep 24, 2017
    Posts:
    6
    After installing the version you suggest (preview 13, version2.1.0), it was fine at the beginning, but one similar problem occurs after a while.
    ```
    MissingReferenceException: The object of type 'Material' has been destroyed but you are still trying to access it.
    Your script should either check if it is null or you should not destroy the object.
    UnityEngine.Object.get_name () (at C:/buildslave/unity/build/Runtime/Export/Scripting/UnityEngineObject.bindings.cs:189)
    TMPro.TMP_SubMeshUI.AddSubTextObject (TMPro.TextMeshProUGUI textComponent, TMPro.MaterialReference materialReference) (at Library/PackageCache/com.unity.textmeshpro@2.1.0-preview.13/Scripts/Runtime/TMP_SubMeshUI.cs:213)
    ```
    I suppose it is because of creating a custom font in a wrong way? But I have no idea figuring out where went wrong.
     
  6. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    When you updated to the new release, did you also re-import the TMP Essential Resources?

    If you did update the TMP Essential Resources and still have the issue and you are able to reproduce it reliably, please submit a bug report with the project so I can take a closer look.

    P.S. Does this error occur when using a prefab?
     
    Last edited: Jun 2, 2020
  7. ChronoWalker

    ChronoWalker

    Joined:
    Sep 24, 2017
    Posts:
    6
    Sorry that I forgot to reimport it when updating to the latest version, and I did it just now (tho I dont know if I reimported correctly, I reimported the whole "TextMesh Pro" directory. )

    And the error occurs after I end my game mode.
     
  8. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    The error occurs when a sub mesh object is being created. Since sub mesh objects are no longer serialized, it would be re-created as you enter / exit playmode but the material reference appears to be getting lost somehow.

    Can you please submit a bug report with the project and steps for me to reproduce this error?
     
    mitaywalle likes this.
  9. mitaywalle

    mitaywalle

    Joined:
    Jul 1, 2013
    Posts:
    247
    Hello! Can we somehow validate materials before, to prevent this error? On per character basis, for example

    Due to production stage we can't update asset at this moment. Work around with TMP files change like bcol made will help?

    Situation:
    - Unity 2019.2.2f1
    - TMP 2.0.1
    - how error (possibly) occur - User enter in TMP.Input + mobile keyboard text with not-from-font characters, then pushed Submit , GenerateTextMesh() executes from MonoBehaviour.Update() and hit error
    - have main baked font and one small dynamic font
    - Error simplified stacktrace (from analytics, no full, sorry):

    Material.HasProperty ()
    TMPro.TextMeshProUGUI.GenerateTextMesh ()
    TMPro.TextMeshProUGUI.OnPreRenderCanvas ()
    TMPro.TextMeshProUGUI.ForceMeshUpdate ()
    ChatMonoBehaviur.TryRebuildTextUpdate ()
    ChatMonoBehaviur.Update ()
     
    Last edited: Aug 3, 2020
  10. jaimelugo

    jaimelugo

    Joined:
    Nov 8, 2019
    Posts:
    27
    I got the same problem just now. Found the TMP with the issue and I changed the font... solved it!
     
    Aotomos likes this.
  11. huulong

    huulong

    Joined:
    Jul 1, 2013
    Posts:
    224