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.

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.

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);
	}
 
}

Changing the rendering mode to either transparent or fade fixed things for me. Same issue, different root cause I suppose

I use the following material with Render Queue Transparent:

85271-screen-shot-2017-01-04-at-120122.png

And a script to set sortingLayer and Order:

myMeshRenderer.sortingLayerName = "Platforms";
myMeshRenderer.sortingOrder = 0;

Awesome workaround
Custom Editor to Expose Sorting Layers

Bump, having the same issue in my VR integration with the OVRVolumeController.

Unity 5.2.1p3 (Oculus Utilities 0.1.3.0).