I have a gameobject with a sprite renderer. and add a (non-ui) text to the sprite as child. So far so good. I can move the object and the text moves along, I can scale the object and the text scales along. So far so good.
But when I change the layer of the object, the text disappears (doesn’t change layers with the object). Besides, I can move other objects between the text-object and the text. I can change the layer of the text to move it closer to the text-object but I can always put other objects in between. Even if the (z)-layer of the text is equal to the text object. It doesn’t matter if I use (legacy) Text, TextMesh or TextMeshPro. They all behave similar.
How do I put text on a sprite that stays with the gameobject no matter what? I need to change the layer of the sprite(renderer) so others appear on top or underneath but with the text. Preferably I don’t want to change the layer of the text separately but if it needs to I can. Just as long as nothing can go between the text and the gameobject.
Any suggestions appreciated.
Thanks for your time.
Three (3) ways that Unity draws / stacks / sorts / layers / overlays stuff:
In short,
The default 3D Renderers draw stuff according to Z depth - distance from camera.
SpriteRenderers draw according to their Sorting Layer and Sorting Depth properties
UI Canvas Renderers draw in linear transform sequence, like a stack of papers
If you find that you need to mix and match items using these different ways of rendering, and have them appear in ways they are not initially designed for, you need to:
identify what you are using
search online for the combination of things you are doing and how to to achieve what you want.
There may be more than one solution to try.
Additional reading in the official docs:
And SortingGroups can also be extremely helpful in certain circumstances:
I have the sprite in sorting groups… but here’s the scenario:
There’s three piles of cards. I keep track of each pile in an array and set the Sorting order according to the index in the array. The highest index in the array lands on top of the pile (conveniently). All works well except for the one card (the last one to select) that has text on it. As soon as I select this card and scale it, it works ok, but when I change the index (and thus the sorting order) because the card lands on top of another pile, the card lands ok but the text disappears (doesn’t move along with the card). The text is child of the card so I’d expect it to behave in the same way as the parent. It does for position, scaling and rotation but not for the sorting order.
The document you suggest doesn’t mention anything about Text(Mesh). If I add a text component straight to the object it doesn’t show at all…?
What you’re suggesting is to put every index on a different layer (all cards at the bottom on layer 0, cards on top of that on layer 1 etc. …? Might be worth looking into.
[Edit] It doesn’t. I have a textcard (gameobject) on sorting layer “0”, sorting order 0, with a child canvas on sorting layer “0” as well, sorting order 1 and that has a child text mesh. Initially the text isn’t shown (?!? why not?) until I move the card to sorting layer “10”, so 9 layers in between. Once you move the card, the text stays visible, moves and scales. All good. When you drop the card and put it back to where it was, the inspector shows both textcard and canvas on sorting layer “0” again. But when I pick up another card from another pile, i.e. from sorting layer “3” and move it to sorting layer “10”, the card is shown on top of the textcard (as it should) but underneath the canvas (=text). So the text can never be covered anymore although it’s on a lower sorting layer. So the layering of the gameobjects themselves work correctly, the text doesn’t. I think I’ll have to create a sprite for every text possibility and make it all graphics. At least that seems to work. Not what I’d hoped though…