- Home /
Setting renderQueue doesn't appear to change draw order
This seems like such a simple thing, judging by the Unity script reference and examples here, but I can't get render queues (set in the material) to work at all. I've set up a very simple scene with two boxes and a camera, one box behind the other (see picture).
Then, I added a script that changes the render queue of the two materials, flipping the queue order. I do a debug afterwards which verifies that the renderQueue value has been changed. The only problem is that the draw order doesn't change - the nearer cube is always rendered over the far cube.
I've included my code below. As you can see from the comments, I tried setting the renderQueue in the renderer.material, sharedMaterial, and the material asset directly, and in all cases the renderQueue value changes but the render order does not. What am I missing here?
using UnityEngine;
using System.Collections;
public class ChangeMaterialQueueOrder : MonoBehaviour {
public GameObject go1;
public GameObject go2;
public Material m1;
public Material m2;
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.F))
{
Debug.Log("Changing 1 to back");
//go1.renderer.sharedMaterial.renderQueue = 1001;
//go2.renderer.sharedMaterial.renderQueue = 1002;
//go1.renderer.material.renderQueue = 1001;
//go2.renderer.material.renderQueue = 1002;
m1.renderQueue = 1001;
m2.renderQueue = 1002;
//Debug.Log("Render queue changed; go1 renderqueue now " + go1.renderer.material.renderQueue);
Debug.Log("Render queue changed; m1 renderqueue now " + m1.renderQueue);
}
if (Input.GetKeyDown(KeyCode.G))
{
Debug.Log("Changing 1 to front");
//go1.renderer.sharedMaterial.renderQueue = 1002;
//go2.renderer.sharedMaterial.renderQueue = 1001;
//go1.renderer.material.renderQueue = 1002;
//go2.renderer.material.renderQueue = 1001;
m1.renderQueue = 1002;
m2.renderQueue = 1001;
//Debug.Log("Render queue changed; go1 renderqueue now " + go1.renderer.material.renderQueue);
Debug.Log("Render queue changed; m1 renderqueue now " + m1.renderQueue);
}
}
}
P.S.: I'm using Unity Pro.
Answer by Julien-Lynge · Nov 21, 2011 at 06:26 PM
Figured this out a while back. Render queue doesn't actually change what items are drawn on top of others, it simply changes in what order they're rendered (so something needed for a reflection, for instance, can be rendered before the thing that needs to reflect it).
The depth buffer (or z-buffer) is what determines what is drawn on top of what, or more correctly how far from the camera things are drawn. This video and the following couple of clips do a great job of describing how the depth buffer works and how to change draw distance for objects:
Thank you very much, this saved me quite some hassle :)!
Your answer
![](https://koobas.hobune.stream/wayback/20220612224923im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Set RenderQueue for Terrain 2 Answers
HDRP DepthMask Possible? 0 Answers
Understanding Render Targets and Render Textures. 1 Answer
Unity2D sprites overlapping 2 Answers
Standard Shader Still Visible through Stencil Shader 0 Answers