# Unity: LineRenderer always behind Sprite

So I'm not sure what I'm missing here -- I created a bullet gameobject with a linerenderer (no sprite renderer) -- and it always renders behind my other sprites, regardless of what I set the z transform to. If I add a sprite renderer component to the bullet, regardless of the sorting layer, the bullet renders behind every sprite. Why is this happening, and how do I fix it? I believe it's due to my material used in the linerenderer -- I have the shader set to particles additive but when I change it to bumped diffuse it renders in front properly (but I want the visual effect to be additive).

I tried setting the sorting layer via script, and I can confirm that the sorting layer is set correctly but the linerender still doesn't render in front of the sprite.

• Please accept the answer you were given when you log in again, by hitting the green checkmark. Thanks. – Engineer Apr 8 '16 at 22:02

Add this script to your LineRenderer GameObject. It will allow you to set the SortingLayer and the SortingOrder in that Layer for your LineRenderer and correctly draw it with an additive material.

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

// Update is called once per frame
void Update () {
if (MyRenderer == null)
MyRenderer = this.GetComponent<Renderer>();
MyRenderer.sortingLayerName = MySortingLayer;
MyRenderer.sortingOrder = MySortingOrderInLayer;

//Debug.Log(MyRenderer.sortingLayerName + " " + MyRenderer.sortingOrder);
}
}

• hey this worked! Do you know why?? I actually tried the exact same thing (setting layer in script) but mine didn't get the same result you did. – Jeff Burghess Jun 9 '15 at 23:55
• I couldn't tell without seeing your code. Personally I'd never tried it until now, I just assumed since Sprites are rendered with an Alpha shader and your LineRenderer is Additive then they must be both rendering in the Transparent pass and Unity was setting the Order of the Sprites higher then the normal shaders. So I looked up Sorting Layer and saw it was attached as a property of Renderer which LineRenderer inherits from so I gave it a try. – Jeremiah Leslie Jun 10 '15 at 0:00
• For performance reasons now that it works you might want to move the stuff out of Update and put it into Start or a custom function you can call so its not being set every frame and only when needed. – Jeremiah Leslie Jun 10 '15 at 0:01
• This didn't worked for me , I'm using the same, but in AWAKE method and still nothing. If you have any solution please post it, since I have the same Issue ! – Karoly Dec 14 '15 at 21:30

Here's what I was able to come up with on Unity 5.6.1, for anyone who is looking for guidance on this in 2017.

public class SetLineRendererSortingLayer : MonoBehaviour
{

public LineRenderer lr;
public string SortingLayer;
public int OrderInLayer;

void Awake()
{
lr = this.GetComponent<LineRenderer>();
}

void Start()
{
lr.sortingLayerName = SortingLayer;
lr.sortingOrder = OrderInLayer;
}

}


Should be able to avoid this problem and many other similar ones by more closely reading about Unity's Sorting Layers.