Use Destroy instead of DestroyImmediate(TMPro_Private)

I am getting this error from TMPro_Private.cs file. I can’t fix the file. Any ideas to work around?

Destroying components immediately is not permitted during physics trigger/contact, animation event callbacks, rendering callbacks or OnValidate. You must use Destroy instead.
UnityEngine.Object:smile:estroyImmediate(Object)

Thanks

What version of Unity are you using?

What version of the TMP package are you using?

I got the same problem.

Unity version 2020.1.0f1
TMP version 3.0.1

What are the steps to reproduce this behavior?

Are you getting this on some prefab object or some objects from an existing scene?

If so, are you getting some debug log message about removing the CanvasRenderer from objects?

1 Like

Yes, I put a prefab with TextMeshPro - Text ( not UI) in the scene.
It show the exception when switch other scene every time.

Thanks for the referring about CanvasRenderer!

After removing the component from the source prefab
here have no exception anymore.

1 Like

In previous versions of Unity, the Graphic class had [RequirementComponent(CanvasRenderer)] which would automatically add a CanvasRenderer to any class inheriting from it like the TMP_Text class. This resulted in a forcing a CanvasRenderer being add to normal objects which don’t need it.

In newer versions of Unity 2019.4, this was changes. As such, when TMP detects an unnecessary CanvasRenderer, it tries to remove it but I guess that can result in issues like you ran into.

The solution is to simply manually remove those unnecessary CanvasRenderers from those objects, prefabs, scenes and re-save them.

1 Like

Finally know why it worked well before upgrading my project.
Hope this thread would help others :wink:

I want to make some addition to the post.
I didn’t have a CanvasRenderer component, but there was an error.

For me, the solution was to remove the TextMeshPro and MeshRenderer components and add them back

1 Like

I had the same issue after the upgrade to 2020.1.
I removed the TextMeshPro and MeshRenderer components and added them back.

The CanvasRenderer should be removed so not sure about the MeshRenderer part.

In hindsight, I will revise this handling to issue a warning indicating that CanvasRenderer should be removed and make the CanvasRenderer visible to make it easy to remove. But I will not be removing it automatically.

In my case there was no Canvas Renderer.
Its the textmesh component over the Entity Object.
After the upgrade, I was getting the error on instantiating those Entity Objects.
After removing and adding these (TextMeshPro & MeshRenderer) components back, the errors stopped.

Thank you.

The CanvasRenderer is hidden. Switch the inspector to debug mode to see if it is visible.

HI,
Its not visible in Debug mode.

Much of this rework (I just had to recreate 10 different prefabs) could be moot if line 78 of TMPro_Private.cs read:
Destroy(canvasRenderer); instead of DestroyImmediate(canvasRenderer);
Of course, I tried doing this myself and it would last mere seconds as the package manager seemed to detect the change and revert it.

I have revised this handling where instead a warning will be issued to notify the user to remove the unnecessary CanvasRenderer from those objects and prefabs. The previously hidden CanvasRenderer will no longer be hidden to make it easier to remove from those objects / prefabs.

This change will be included in the next package release which I am hoping to have available sometime this week.

1 Like

That would be great.