LineRenderer appear behind sprites

I am making a 2D game.

I have my back ground created with sprites. The material of the sprites is Sprites-Default. The sprites have a sorting layer and order in layer.

I have a LineRenderer in the scene and it is appearing behind the sprites. LineRenderer material is using Particales/ Additive. I want the LineRenderer to appear over the sprites.

Any advice on how to fix this?

You could also set the LineRender to a “higher” sorting layer.

line.sortingLayerName = "Foreground";

Hello,

To use the property “sortingLayerName” and “sortingOrder”, your line must have a material with a shader “Sprite/*” :wink:

stop using sorting order, use z

sprite os for 2d and linerenderer is for 3d these two systems aren’t fully compatible, we solved all problems we had with sprites by setting layer order to 0 and we use z as z-index and it works well for us

Or try this (you need a SpriteRenderer component in the same GameObject)

    LineRenderer line;
	// Use this for initialization
	void Start () {
		line = GetComponent<LineRenderer>();
		SpriteRenderer spriteRenderer = GetComponent<SpriteRenderer>();
		line.sortingLayerID = spriteRenderer.sortingLayerID;
		line.sortingOrder = spriteRenderer.sortingOrder;
	}

This is most likely if you used the default shader material. Change the shader material to a sprite shader to fix.

Hey,

Here are my scripts that solve this issue:

using UnityEngine;

[RequireComponent (typeof (LineRenderer))]
[ExecuteInEditMode]

public class LineRendererLayer : MonoBehaviour
{
	public string sortingLayer;

    private Renderer getMeshRenderer()
	{
		return gameObject.GetComponent<Renderer>();
	}
	
	void Update()
	{
		if(getMeshRenderer().sortingLayerName != sortingLayer && sortingLayer != ""){
			//Debug.Log("Forcing sorting layer: "+sortingLayer);
			getMeshRenderer().sortingLayerName = sortingLayer;
		}
	}
}

And here is My Editor script to make it easier:

using UnityEditor;
using System.Reflection;
using System;
using UnityEditorInternal;

[CustomEditor (typeof(LineRendererLayer))]
public class LineRendererLayerEditor : Editor 
{
	string[] layers;
	public LineRendererLayerEditor()
	{
		Type internalEditorUtilityType = typeof(InternalEditorUtility);
		PropertyInfo sortingLayersProperty = internalEditorUtilityType.GetProperty("sortingLayerNames", BindingFlags.Static | BindingFlags.NonPublic);
		layers=(string[])sortingLayersProperty.GetValue(null, new object[0]);
	}


	public override void OnInspectorGUI() 
	{
		LineRendererLayer myTarget=(LineRendererLayer)target;
		//EditorGUI.EnumPopup(new Rect(0,0,100,50),layers);
		int selectedIndex;
		if(myTarget.sortingLayer=="")
		{
			myTarget.sortingLayer=layers[0];
			selectedIndex=0;
		}
		else
		{
			selectedIndex=Array.IndexOf<string>(layers,myTarget.sortingLayer);
			if(selectedIndex==-1)
			{
				myTarget.sortingLayer=layers[0];
				selectedIndex=0;
			}
		}
		myTarget.sortingLayer=layers[EditorGUILayout.Popup(selectedIndex,layers)];
        myTarget.enabled = false;
        myTarget.enabled = true;
	}
}

Hope these will help somebody :slight_smile: