make 3d mesh follow a sorting layer and stay in front of a 2d background

hi :):):slight_smile: i have a problem with sorting layer on a 3dmesh.

i have 3d object i want to render beyond a 2d background, that still have sprite render script with sorting order assigned to -20.
the 3d object have this script assinged that i have found on the forum:

    using UnityEngine;
    using System.Collections;
    
    public class SetSortingLayer : MonoBehaviour {
    	
    	public string sortingLayerName;       // The name of the sorting layer .
    	public int sortingOrder;      //The sorting order
    	
    	void Start ()
    	{
    		// Set the sorting layer and order.
    		renderer.sortingLayerName = sortingLayerName;
    		renderer.sortingOrder=sortingOrder;
    	}
    }

i set up the layer order to -30.
but how you can see on the image i have posted, the cube are rendered in front off the 2d Background.

is possible for a 3d object(mesh render) stay at the same layer order of a sprite rendered?

I went to test it just now because i was sure is possible but turns out it doesn’t work with primitives, strangely enough it works with TextMesh, which uses a mesh renderer like any primitive, maybe try with a custom mesh or a script generated one.

nope…i have test this morning with a 3d mesh i have made and a material with ZWrite on and Off…but nothing affect the layerorder. With “Zwrite on” the mesh was visibile. “with Zwrite of” is invisibile. every time i put my hands on Unity thinking “right now i have understand!” there something found out that make me to think, i don’t understand at all in the end! :sad: Neurological are you Italian?

If you want to use ZWrite, then you need to use it in the Sprite materials as well. Edit your sprite shader so it has ZWrite ON as well, and this will work.

no inverte still not working. i give “Zwrite on” on sprite and on a 3d mesh…but still not follow layer order.
if i use all rendered sprite instead a 3d object (mesh Rendered), that will works fine. can you give me an example unityscene with 2d and 3d mesh follow the layer order? in that case i can figure out what i have to do…if is possible.

Sorry for the late reply didn’t notice you replied. Yes I’m italian.