Search Unity

TextMesh Pro NullReferenceException on disabled canvas with TMP_Text component

Discussion in 'UGUI & TextMesh Pro' started by Garrafote, Apr 24, 2020.

  1. Garrafote

    Garrafote

    Joined:
    Jun 13, 2013
    Posts:
    48
    Hi, found an issue after upgrading Text Mesh Pro from 2.0.1 to 3.0.0-preview11.

    I'm getting a NullReferenceException on TextMeshProUGUI when I disable my canvas component.

    Code (CSharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. TMPro.TextMeshProUGUI.GetCanvasSpaceClippingRect () (at Library/PackageCache/com.unity.textmeshpro@3.0.0-preview.11/Scripts/Runtime/TMPro_UGUI_Private.cs:4499)
    3. TMPro.TextMeshProUGUI.Cull (UnityEngine.Rect clipRect, System.Boolean validRect) (at Library/PackageCache/com.unity.textmeshpro@3.0.0-preview.11/Scripts/Runtime/TextMeshProUGUI.cs:374)
    4. UnityEngine.UI.RectMask2D.PerformClipping () (at D:/Software/Unity/2020.1.0b6/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/RectMask2D.cs:269)
    5. UnityEngine.UI.ClipperRegistry.Cull () (at D:/Software/Unity/2020.1.0b6/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/Culling/ClipperRegistry.cs:46)
    6. UnityEngine.UI.CanvasUpdateRegistry.PerformUpdate () (at D:/Software/Unity/2020.1.0b6/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/CanvasUpdateRegistry.cs:197)
    7. UnityEngine.Canvas.SendWillRenderCanvases () (at <54491e60d4894975bc04f0a9481064bb>:0)
    8. UnityEngine.Canvas.ForceUpdateCanvases () (at <54491e60d4894975bc04f0a9481064bb>:0)
    9. UnityEditor.DrivenRectTransformUndo.ForceUpdateCanvases () (at <148351c941224bb7889deeaf4e8d9b1f>:0)
    10. UnityEditor.Undo.Internal_CallWillFlushUndoRecord () (at <148351c941224bb7889deeaf4e8d9b1f>:0)
    Code (CSharp):
    1.  
    2.         internal override Rect GetCanvasSpaceClippingRect()
    3.         {
    4.             // m_canvas is null
    5.             Transform rootCanvasTransform = m_canvas.rootCanvas.transform;
    6.             Bounds compoundBounds = GetCompoundBounds();
    7.  
    8.             Vector3 position =  rootCanvasTransform.InverseTransformPoint(m_rectTransform.position);
    9.  
    10.             return new Rect(position + compoundBounds.min, compoundBounds.size);
    11.         }
    12.  
     
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Makes total sense as per my FYI below. Should be an easy fix once I can reproduce it.

    Do you have layout components in there in addition to some RectMask2D?

    Can you provide an example of simple UI setup that reproduces the issue?

    I'll publish the code changes later tonight or more than likely release Preview 12 which will include this fix plus another minor one related to Raycast Targets sync on sub mesh objects.

    FYI: If your workflow involves disabling canvases, be mindful that when a Canvas is disabled in the UI System, checking the canvas property of UI Elements that derive from Graphic which is most of them, will return null although the canvas is still there.
     
    Last edited: Apr 24, 2020
  3. POiD96

    POiD96

    Joined:
    Aug 16, 2016
    Posts:
    4
    I'm hitting this same situation in preview 11 and in preview 10. Downgrading to preview 8 removed the issue.

    Under 10/11, my UI just wouldn't show, canvas enabled or disabled. My workflow indeed involved disabling canvases when they were not used.
     
  4. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    I am assuming that you have some RectMask2D somewhere as parent of the text object throwing this error. Correct?

    I need some repro project to be able to reproduce this. Can you please submit a bug report with the project and steps to reproduce?

    I would like to get this addressed quickly for Preview 12 which I would like to release over the weekend.

    P.S. Adding a null check in the GetCanvasSpaceClippingRect() would likely resolve the issue but before doing so, I would still like to be able to reproduce the behavior in case it reveals some other potential issue.
     
    Last edited: May 1, 2020
  5. voporak5

    voporak5

    Joined:
    Jul 1, 2013
    Posts:
    31
    Any updates on this?
     
  6. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Preview 12 has been released so please see if you still have this issue.
     
  7. voporak5

    voporak5

    Joined:
    Jul 1, 2013
    Posts:
    31
    that's great, I don't see the issue immediately reproducing now! is it recommended that we use the GUIID version remapping tool? if going from preview 8 to preview 12?
     
  8. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    The remapping tool was only needed / used when migrating from Legacy versions from the AssetStore that predated the introduction of the Package Manager in 2018.x.
     
    voporak5 likes this.
  9. hazard_fn

    hazard_fn

    Joined:
    May 13, 2020
    Posts:
    1
    I am still seeing this error in preview 12:


    Code (CSharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. TMPro.TextMeshProUGUI.GetCanvasSpaceClippingRect () (at Library/PackageCache/com.unity.textmeshpro@2.1.0-preview.12/Scripts/Runtime/TMPro_UGUI_Private.cs:4510)
    3. TMPro.TMP_SubMeshUI.Cull (UnityEngine.Rect clipRect, System.Boolean validRect) (at Library/PackageCache/com.unity.textmeshpro@2.1.0-preview.12/Scripts/Runtime/TMP_SubMeshUI.cs:651)
    4. UnityEngine.UI.RectMask2D.PerformClipping () (at /Applications/Unity/Hub/Editor/2019.3.12f1/Unity.app/Contents/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/RectMask2D.cs:220)
    5. UnityEngine.UI.ClipperRegistry.Cull () (at /Applications/Unity/Hub/Editor/2019.3.12f1/Unity.app/Contents/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/Culling/ClipperRegistry.cs:46)
    6. UnityEngine.UI.CanvasUpdateRegistry.PerformUpdate () (at /Applications/Unity/Hub/Editor/2019.3.12f1/Unity.app/Contents/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/CanvasUpdateRegistry.cs:197)
    7. UnityEngine.Canvas.SendWillRenderCanvases () (at /Users/builduser/buildslave/unity/build/Modules/UI/ScriptBindings/UICanvas.bindings.cs:72)
    8.  
    Similarly, downgrading to preview 8 works fine.
     
  10. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Can you please submit a bug report on this or provide me with the steps to reproduce this behavior?

    I now suspect this occurs when using nested canvas along with a RectMask2D somewhere in that hierarchy and maybe the root or child canvas being disabled.

    P.S. Please make the following changes and let me know if the issue still occurs.

    upload_2020-5-13_13-43-20.png
     
  11. Sly88

    Sly88

    Joined:
    Feb 22, 2016
    Posts:
    73
    @Stephan_B hi, I have the same problem. I use Unity 2019.3.14f and TextMeshPro preview.13 - 2.1.0. any advice on how to fix it?
     
  12. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    This above code fix I posted will resolve that issue. This fix will be included in Preview 14 which I am planning on releasing later today.
     
    Sly88 likes this.
  13. Sly88

    Sly88

    Joined:
    Feb 22, 2016
    Posts:
    73
    @Stephan_B Hi, I tested a new package (preview 14 2.1.0) and now I have a new issue for example when I use sprite in TextMeshPro - Text(UI) I have a lot of extra space around in the label it happens also for texts without sprites. Do you know what causes this issue? works fine on previous packages
     
  14. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Can you post an image of what you are seeing?

    Are you using any type of layout components or are you observing this on a standalone text object?

    What version of TMP were you using before?
     
  15. Sly88

    Sly88

    Joined:
    Feb 22, 2016
    Posts:
    73
    hi, before I used preview 12 and 13. Parent have only Layout Element Component
     

    Attached Files:

  16. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Is this a sprite asset that you were using before or a newly created sprite asset?

    Can you provide me / export the sprite asset with its dependencies for testing?
     
  17. Sly88

    Sly88

    Joined:
    Feb 22, 2016
    Posts:
    73
    it is the same asset. After downgrade the plugin with the same properties everything works fine.
     

    Attached Files:

  18. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Can you provide me with a project that includes a scene and the sprite asset so I can take a closer look?
     
  19. Sly88

    Sly88

    Joined:
    Feb 22, 2016
    Posts:
    73
    yeah, the same case for sprites from TextMeshPro example. Positions in preview 13 and 14 are different.
     

    Attached Files:

  20. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    I'll take a look tomorrow and follow up as soon as I have more information.
     
    Sly88 likes this.
  21. Sly88

    Sly88

    Joined:
    Feb 22, 2016
    Posts:
    73
    hi, any news?
     
  22. Garrafote

    Garrafote

    Joined:
    Jun 13, 2013
    Posts:
    48
    @Stephan_B: Apologies for not getting back to you as I did not realize I wasn't watching this thread.

    Just wanted to say that updating to preview 13 fixed the problem on my end.
     
    Stephan_B likes this.
  23. Garrafote

    Garrafote

    Joined:
    Jun 13, 2013
    Posts:
    48
    Hi again @Stephan_B,

    The error reappeared after updating to preview 14 but strangely enough it now only happens if I disable my canvas on Start and there is a 3D Text Mesh object in the scene. And after the canvas is enabled for the first time I can disable it safely.

    I created a small project with the problem so you can reproduce yourself.

    1. Open the Sample Scene on Unity 2020.1.0b12 and play

    you should get the following error every frame:
    Code (csharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. TMPro.TextMeshProUGUI.GetCanvasSpaceClippingRect () (at Library/PackageCache/com.unity.textmeshpro@3.0.0-preview.14/Scripts/Runtime/TMPro_UGUI_Private.cs:4482)
    3. TMPro.TextMeshProUGUI.UpdateCulling () (at Library/PackageCache/com.unity.textmeshpro@3.0.0-preview.14/Scripts/Runtime/TextMeshProUGUI.cs:414)
    4. TMPro.TMP_UpdateManager.DoRebuilds () (at Library/PackageCache/com.unity.textmeshpro@3.0.0-preview.14/Scripts/Runtime/TMP_UpdateManager.cs:184)
    5. UnityEngine.Canvas.SendWillRenderCanvases () (at <54491e60d4894975bc04f0a9481064bb>:0)
    6.  
    2. Press escape to enable/disable the canvas

    The error should stop happening.
     

    Attached Files:

    Stephan_B likes this.
  24. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Thank you for providing a repro project this will make is easy to resolve :)

    I'll take a look this afternoon and provide feedback as soon as I have more information.

    Update
    I have resolved the issue which is timing related as Unity goes through assembly reload where some of the Canvas functions get called before the text objects are fully awake. This was a simply fix but I wanted to understand why this was happening.

    Fix will be in the next release of the TMP package. Next releases will be non preview releases and version 1.5.0 for Unity 2018.4, version 2.1.0 for Unity 2019.x and version 3.0.0 for Unity 2020.x.
     
    Last edited: Jun 18, 2020
  25. Sly88

    Sly88

    Joined:
    Feb 22, 2016
    Posts:
    73
    hi @Stephan_B when we can expect a new patch?
     
  26. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Next releases should be non preview releases which I would like to publish over the weekend.
     
    Sly88 likes this.
  27. Sly88

    Sly88

    Joined:
    Feb 22, 2016
    Posts:
    73
    hi @Stephan_B any news about the update?
     
  28. Garrafote

    Garrafote

    Joined:
    Jun 13, 2013
    Posts:
    48
    Update:

    Upgrading to TextMeshPRO 3.0.0 fixed it for me.
     
    Stephan_B likes this.