- Home /
Unity 5: MeshRenderer and SortingLayer not working?
Hello!
I am creating a 2D platformer, I have MeshRenderers (for the tiles) and SpriteRenderers (single objects) co-existing.
For the sprites of course the sorting layers and sorting orders work perfectly. For the MeshRenderers I am able to set their sorting layers and sorting orders in a script as many people suggest, however they are still drawn behind all my sprites regardless of the sorting layer I put them in. It seems that this was working fine until Unity 5 but is not working anymore.
Does anyone know how to make it work or what I am doing wrong?
P.S.: I have put Zwrite Off in my shader as I believe this is the right thing to do when using sorting layers instead of Z ordering. Additionally, if I put Zwrite On then my meshes are drawn on top of all my sprites.
Answer by GiyomuGames · Oct 13, 2015 at 11:23 PM
Edit: I have got the definite answer thanks to Unity QA team. This is what they told me to do and it worked perfectly:
To solve this issue set Inspector to debug mode (right click on inspector tab and select debug). After doing that select "Tiles Normal Map" and "Sprites Normal Map" and change their Custom Render Queue so that both would be the same.
"Tiles Normal Map" being the material I use for my tiles using a MeshRenderer and "Sprites Normal Map" the material I use for my regular sprites. They both use the same shader but a different texture and normal map. When I checked, indeed they didn't have the same render queue which explains why the ordering wasn't working at all.
Thanks Unity QA!
Hello there.
I haven't found a way to make the MeshRenderers work with the Sprites and the sorting layers. Therefore I have used a work around which consists in using different cameras with different depths to ensure a correct drawing order.
I am still interested in a way to sort all that without using several cameras so don't hesitate to reply later.
When you say you're setting it "in a script", how exactly are you doing that? Can you show the exact code and logic that does that?
I use Sorting Layers via script all the time and I've not experienced any problems. But I never set them by name. I just copy values from a SpriteRenderer.
Hi Pharan,
I am simply doing:
my$$anonymous$$eshRenderer.sortingLayerName = "Platforms";
my$$anonymous$$eshRenderer.sortingOrder = 0;
I have also a plugin which makes this visible in the editor and therefore I can confirm that the sorting layer name and order have been set correctly.
I think I have the same issue but I can't for the life of me find "Tiles Normal $$anonymous$$ap" and "Sprites Normal $$anonymous$$ap"??
Answer by gapMindful · Mar 21, 2016 at 09:45 AM
Changing the rendering mode to either transparent or fade fixed things for me. Same issue, different root cause I suppose
Answer by Cherno · Oct 14, 2015 at 09:04 AM
Try this script on the object:
using UnityEngine;
[ExecuteInEditMode]
public class SetSortingLayer : MonoBehaviour {
public Renderer MyRenderer;
public string MySortingLayer;
public int MySortingOrderInLayer;
// Use this for initialization
void Start () {
if (MyRenderer == null) {
MyRenderer = this.GetComponent<Renderer>();
}
SetLayer();
}
public void SetLayer() {
if (MyRenderer == null) {
MyRenderer = this.GetComponent<Renderer>();
}
MyRenderer.sortingLayerName = MySortingLayer;
MyRenderer.sortingOrder = MySortingOrderInLayer;
//Debug.Log(MyRenderer.sortingLayerName + " " + MyRenderer.sortingOrder);
}
}
Hi Cherno, sorry I just saw your comment! I am pretty sure I tried that. Do you confirm that it works for you with Unity 5 and above?
This sets the order in layer in the editor but doesn't stop all z-fighting. If I move the dark brown layer to the same layer as the green it will overlap it completely, but when it's back 1 layer it draws mostly behind except for 2 lines which stay in front. This is quite strange as it's the middle of the brown quad - there is no edge there - I can't figure it out.
As you can see by the z-fighting in the editor the brown quad and green quads are set to the same z transform value (0), while the light green and black are both z=1, and they both clip in the same way.
As the camera moves up and down the lines flicker terribly. The brown and black layers are both layer 13 while the green layers are both layer 14 which should always render in front.
Changing all layers to transparent fixes the problem and also allows layers that are further back in z order to overlap those in front as well which could make for some interesting effects.. I'm guessing turning z-write off for all shaders would give the same effect.
That fixed the issue for me, didn't know there is a sorting layer for a render that is not spriteRenderer.. was looking for a fix for that issue for a long time, so thanks a lot.
Answer by tucano · Jan 04, 2017 at 11:25 AM
I use the following material with Render Queue Transparent:
And a script to set sortingLayer and Order:
myMeshRenderer.sortingLayerName = "Platforms";
myMeshRenderer.sortingOrder = 0;
Answer by WMorais · Apr 30, 2019 at 09:15 PM
Awesome workaround Custom Editor to Expose Sorting Layers
tried it on 2021.1.3f1 and no results ,just put it in a simple two cubes with simple camera
Your answer
![](https://koobas.hobune.stream/wayback/20220612033311im_/https://answers.unity.com/themes/thub/images/avi.jpg)