0
votes

So I'm having an issue that sounds like it should be relatively easy to fix, and it might be and I'm just going about this all wrong. My Game uses the Sorting Layer elements on all of my sprites, so that they don't get hidden behind others.

I'm trying to create a TextMeshPro UI object (just simple text) but because of these sorting layers I have setup, it is always behind the other sprites. I want the text to be in front of all the sprites. I've tried setting up a script to do this, but I'm not completely sure where to point this script. Most answers say to a Mesh Renderer, but that does not seem to work for me.

Any suggestions are appreciated, thanks!

Edit: The text is grouped with another sprite in the hierarchy

Edit 2: By request of Darkonekt I'll try to add a some more explanation. Steps I have found to replicate:

  1. After creating a new project (2D), dd the TMPro asset (I did this by adding a Text Mesh Pro UI object to the scene and importing just the essentials.)
  2. Put any form of sprite in the scene (make sure it is a child to the canvas)
  3. Add the Text Mesh Pro UI -Text Object, make sure it is a child to the canvas
  4. Set the Canvas's render mode to "Screen Space - Camera" and set the render camera to the Main Camera
  5. You might need to re-size the sprite, make sure it takes up most of the screen
  6. Back on the sprite, in the Sprite Renderer Component, click sorting layer then add sorting layer
  7. Name it whatever you want, and make sure it is below Default
  8. On the sprite again, set thew Sorting Layer to Front, the text should have disappeared behind it.

There isn't any code to show, because none of the listed values above are not changed by any code.

Sorting Layer On Back

Sorting Layer On Front

1
Are your sprites Sprite components or they all are UI components? If they all are UI components you must take care about the order in hierarchy, cause Unity draw all UI objects in the same order they are in the hierarchy, last UI element in the hierarchy is drawn over all the rest.Windgate
You could try to modify the canvas layers settings. I guess TextMeshPro is render through itLucas Gras
@Windgate They are all in the UI, and reording them may work, but I forgot to mention something (I edited the question) the text is grouped with some other sprites. I guess what I could try is to create an empty object and put the text in there and rearrange it.HelpMeGame
We need to see code and pictures of your inspector so that we see what the actual problem is.... Also if you can upload a reproduceable example it would be good.Jonathan Alfaro
@Darkonekt Ok thanks, I edited the post again to hopefully make it a little easier to understandHelpMeGame

1 Answers

2
votes

For this to work you have to make sure of a few things:

  1. The Z axis of the Sprite's transform's position is 0 in the inspector.
  2. The Canvas Sorting Layer is also Front (same as the sprite). This setting becomes available when you set the Canvas Render Mode to "Screen Space - Camera".
  3. The Text Mesh Pro object's Rect Transform's position's Z axis is 0.

That should help you achieve the result you want.

The reason this happens is because when you set the Sprite's Sorting Layer to "Front" it becomes positioned on top of everything that is on the Default layer.

Since the Canvas is now set to the Default layer then everything in the Canvas is also positioned in the Default layer unless explicitly specified.