- Home /
Unity2D - Changing sorting order on text mesh makes text disappear??
I'm instantiating pickup sprites with a child gameobject that has a text mesh attached to it every two seconds. I'm trying to make it such that the text appears above the sprite but NOT above the sprites that get drawn after it. So I'm setting the sorting order for the text as 1 above the sorting order of its sprite, then incrementing the layer order number by TWO every time (so the next sprite overlaps the text of the last).
The result is very weird! Firstly, the sorting order for the sprites increments fine, but the text that's attached to them does not have a sorting order one higher than the sprite's, but equal to it instead (see console for text sorting order and inspector for sprite sorting order for the last object created). Secondly, only the first object which spawns has its text drawn above it (the cloud with number 4 above!). The rest of them do not have any visible text (I assume it is being drawn below the sprite.)
I can make the text for all objects appear over the sprites if I put the sorting order = 0 (default), but then all text is above all objects, which I do not want.
I have already checked http://answers.unity3d.com/questions/595634/3d-textmesh-not-being-drawn-properly-in-a-2d-game.html This helped me learn how to access .sortingOrder and change it, but is not giving me the result I need. I'd really appreciate any help here, I've been so stuck on this for so long!!
PS: This is the first time I've posted so please let me know if I'm unclear or have put this question in the wrong place :)
void Start()
{
layerOrder = 1;
}
// Update is called once per frame
void Update()
{
timeSinceSpawn += Time.deltaTime;
//spawn new pickup every 2 seconds
if (timeSinceSpawn > 2)
{
Rigidbody2D newPickup = Instantiate(pickup);
//set draw order for pickup
newPickup.GetComponent<SpriteRenderer>().sortingOrder = layerOrder;
//find text mesh component on child
Transform gameObjChild = newPickup.GetComponentInChildren<Transform>();
//find text mesh renderer
Renderer childRenderer = gameObjChild.GetComponent<Renderer>();
//set draw order for text on pickup. Must be 1 above so it can be seen over the pickup.
childRenderer.sortingOrder = layerOrder + 1;
Debug.Log("Text sorting order: ");
Debug.Log(childRenderer.sortingOrder);
layerOrder += 2;
timeSinceSpawn = 0;
}
}
Answer by Inune · Dec 06, 2016 at 08:43 PM
I've found some kind of a workaround which has done the job for me, though it might not be the best fix. I realized that the reason the text mesh had the same sorting order as the sprite (despite my code setting it as one above the sprite) was because it was a child of the sprite, and apparently children and parents must have the same sorting order!
So I changed the text mesh from being a child of the sprite to being its own gameobject, and made a prefab from that which I instantiate separately from the sprite. Then changing its sorting order works perfectly and I get the text underneath the sprites that spawn after it, but above the sprites that spawn before it.
Here's the final code:
void Start()
{
layerOrder = 1;
}
// Update is called once per frame
void Update()
{
timeSinceSpawn += Time.deltaTime;
//spawn new pickup every 2 seconds
if (timeSinceSpawn > 2)
{
//Instantiate sprite and set sorting order as before
Rigidbody2D newPickup = Instantiate(pickup);
newPickup.GetComponent<SpriteRenderer>().sortingOrder = layerOrder;
//Instantiate text object and set sorting order as 1 above sprite
GameObject newText = Instantiate(pickupText);
Renderer textRenderer = newText.GetComponent<Renderer>();
textRenderer.sortingOrder = layerOrder + 1;
layerOrder += 2;
timeSinceSpawn = 0;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612102035im_/https://answers.unity.com/themes/thub/images/avi.jpg)