Search code examples
c#unity-game-enginetextmeshprounity-components

TextMeshPro Text only renders on some part of the game screen


I am trying to add TextMeshPro on a game object(sprite) outside the UI in 2D world. So I created a sprite to act as the background and parent game object then I put a canvas as the child of the sprite. Finally I put the TextMeshPro as the child of the canvas. I set the canvas render mode to world space. I am programmatically adding the sprite game object to the scene. The TextMeshPro on some portions of the screen does not show its text but when I move it around, the text shows on other portions of the screen.

Node GameObject

Above is the structure of game object, the node is the sprite.

enter image description here

This is the result of the TextMeshPro. Node 1,2 and 3 are showing the text but Node 4 and 5 are not. If I move Node 4 and 5 to the area where Node 1,2 and 3 are, it shows its text.


Solution

  • Put the canvas and the sprite in the same sorting layer and give the canvas a bigger order, this will make the UI elements appear in front of the sprite.

    Sorting layer order

    If the canvas and the sprite have the same order, the display order is related to their distance from the camera.