- Home /
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?
Answer by komodor · Apr 23, 2014 at 11:41 AM
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
I tried it and it seems to be working ...
if i move the background forward (z to 10) keeping LineRenderer at z 0 I get the behavior i need.
But if I move the LineRenderer backward (z to -10) keeping the background at z 0 I don't get the same behavior.
Any idea why?
i am not sure what you mean
camera shows closer objects over the distant objects so if linerenderer is closer then background, then you should see it over the background
The problem was the points I add to the LineRenderer had the z value 0 even though the LineRenderer was at -10. I moved them backward to -10 and it is working now
You should be able to set all of your z values to 0, and use layers to keep game objects on top that need to be rendered on top. This is discussed in the 2D tutorials.
Like Joe Van Richter said, set the sortingLayerName in the line renderer. Do this in your Start() method:
line.sortingLayerName = "Your layer where it should go";
I've tried to use the solution of Joe van Richter, but somehow it doesn't work. I also changed the material and the shader, but it didn't help. What I noticed although that it doesn't matter what I set in the Start() method, the sorting layer always gets back to "Default". This is a test scene particularly for that problem so I don't know what's going on. Do you have any idea maybe?
But the solution suggested by @komodor works.
When I change the z of the positions of linerenderer to -1, it just disappears.
Answer by Joe van Richter · Aug 19, 2014 at 05:14 PM
You could also set the LineRender to a "higher" sorting layer.
line.sortingLayerName = "Foreground";
That's the right way to do it. I had to set the line's material as well, otherwise it didn't have any effect.
This worked for me also. As AceDBA44 mentioned, have to set the line material. "Default Line" worked just fine. Thanks Joe van Richter
Answer by fabien0102 · Apr 15, 2016 at 09:04 AM
Hello,
To use the property "sortingLayerName" and "sortingOrder", your line must have a material with a shader "Sprite/*" ;)
That's not true. They're only exposed in the inspector by default for sprites, but you can use them via script on any renderers.
It actually fix my problem, when using the standard shader I coudn't get the lines to show on top of the other sorting layers. Thanks!
Sorting layer / order was first thing which came to my $$anonymous$$d, but my material was using standard shader. This fixed my problem. Thanks!
Answer by Mikael-H · Aug 03, 2014 at 08:11 PM
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;
}
Answer by DukeW · Sep 01, 2015 at 11:07 AM
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 :-)
Your answer
![](https://koobas.hobune.stream/wayback/20220613143407im_/https://answers.unity.com/themes/thub/images/avi.jpg)