Updated for Unity 2022/2023 LTS and Unity 6
Symptoms
- Objects may appear in unexpected depths
- Order in Layer is ignored i.e. in Sprite Renderer
Causes
Most often a script (Editor or runtime) assigns Material.renderQueue, and that material later gets reused with the expectation that it still follows the shader’s default queue:
gameObject.GetComponent<Renderer>().sharedMaterial.renderQueue = 2000;Duplicating or re-importing such a material preserves the overridden queue, so depth ordering breaks.
Resolution
Reset each material's renderQueue to material.shader.renderQueue.
The following Editor script iterates over every saved material asset and restores each material's render queue to its shader's default:
#if UNITY_EDITOR
using UnityEditor;
using UnityEngine;
public static class ResetMaterialsRenderQueue
{
[MenuItem("Assets/Reset Materials RenderQueue")]
private static void ResetRenderQueues()
{
string[] guids = AssetDatabase.FindAssets("t:Material");
int total = guids.Length;
for (int i = 0; i < total; i++)
{
string path = AssetDatabase.GUIDToAssetPath(guids[i]);
Material mat = AssetDatabase.LoadAssetAtPath<Material>(path);
if (mat != null && mat.shader != null &&
mat.renderQueue != mat.shader.renderQueue)
{
mat.renderQueue = mat.shader.renderQueue;
EditorUtility.SetDirty(mat);
}
if (EditorUtility.DisplayCancelableProgressBar(
"Reset Materials RenderQueue",
$"{i + 1}/{total}: {path}",
(float)(i + 1) / total))
{
break;
}
}
EditorUtility.ClearProgressBar();
AssetDatabase.SaveAssets();
}
}
#endif
Further Instructions
- Download the script.
- Place it in
Assets/Editor/. - In Unity Editor, open the Assets menu and select Reset Materials RenderQueue.