NullReferenceException on disabled canvas with TMP_Text component

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.

NullReferenceException: Object reference not set to an instance of an object
TMPro.TextMeshProUGUI.GetCanvasSpaceClippingRect () (at Library/PackageCache/com.unity.textmeshpro@3.0.0-preview.11/Scripts/Runtime/TMPro_UGUI_Private.cs:4499)
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)
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)
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)
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)
UnityEngine.Canvas.SendWillRenderCanvases () (at <54491e60d4894975bc04f0a9481064bb>:0)
UnityEngine.Canvas.ForceUpdateCanvases () (at <54491e60d4894975bc04f0a9481064bb>:0)
UnityEditor.DrivenRectTransformUndo.ForceUpdateCanvases () (at <148351c941224bb7889deeaf4e8d9b1f>:0)
UnityEditor.Undo.Internal_CallWillFlushUndoRecord () (at <148351c941224bb7889deeaf4e8d9b1f>:0)
        internal override Rect GetCanvasSpaceClippingRect()
        {
            // m_canvas is null
            Transform rootCanvasTransform = m_canvas.rootCanvas.transform;
            Bounds compoundBounds = GetCompoundBounds();

            Vector3 position =  rootCanvasTransform.InverseTransformPoint(m_rectTransform.position);

            return new Rect(position + compoundBounds.min, compoundBounds.size);
        }

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.

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.

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.

Any updates on this?

Preview 12 has been released so please see if you still have this issue.

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?

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.

1 Like

I am still seeing this error in preview 12:

NullReferenceException: Object reference not set to an instance of an object
TMPro.TextMeshProUGUI.GetCanvasSpaceClippingRect () (at Library/PackageCache/com.unity.textmeshpro@2.1.0-preview.12/Scripts/Runtime/TMPro_UGUI_Private.cs:4510)
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)
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)
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)
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)
UnityEngine.Canvas.SendWillRenderCanvases () (at /Users/builduser/buildslave/unity/build/Modules/UI/ScriptBindings/UICanvas.bindings.cs:72)

Similarly, downgrading to preview 8 works fine.

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.

5844268--620569--upload_2020-5-13_13-43-20.png

@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?

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.

1 Like

@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

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?

hi, before I used preview 12 and 13. Parent have only Layout Element Component

5971502--640835--Screen Shot 2020-06-12 at 12.41.31.png
5971502--640838--Screen Shot 2020-06-12 at 12.42.01.png

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?

it is the same asset. After downgrade the plugin with the same properties everything works fine.

5975687--641546--clock_icon.png
5975687--641549--Screen Shot 2020-06-13 at 13.23.14.png
5975687--641552--Screen Shot 2020-06-13 at 13.23.35.png

Can you provide me with a project that includes a scene and the sprite asset so I can take a closer look?

yeah, the same case for sprites from TextMeshPro example. Positions in preview 13 and 14 are different.

5981378–642545–Archive.zip (1.28 MB)
5981378--642539--prev13.png
5981378--642542--prev14.png
5981378–642545–Archive.zip (1.28 MB)

I’ll take a look tomorrow and follow up as soon as I have more information.

1 Like