Trail Renderer and Sorting Layer problem

It’s a 2D game. I have a bullet with a trail renderer and the trail renderer is visible in the #Scene but it isn’t visible in the Game screen of the Unity. When I remove background of the game, it shows the trail renderer. So I figured out it’s sorting layer problem with trail renderer.

I tried renderer.sortingLayerName and renderer.sortingOrder, but no effect. (It worked on particle system).

How to set sorting layer to trail renderer ? or Is there any alternative way to render trail of the bullet?
Thank you.

Finally I have sorted it out. (Trail renderer has own renderer, I put there sortingLayerName.) I attached following new script to bullet’s parent. And it works as I expected. Whoever has a same problem, try it.

public TrailRenderer trail;
// Use this for initialization
void Start () {
	trail.sortingLayerName = "Background";
	trail.sortingOrder = 2;

}

Make sure the material of your trail renderer has a shader of type Sprites/Default, otherwise sorting layers wont work

Just to add onto this thread in case somebody else comes across it. It seems like there are a number of problems you can encounter when adding a trail renderer to a sprite and not having it display.

Eventually I found out what was going on in my case. I was adding a trail renderer to a sprite that was (obviously) on a canvas. The canvas property “Render Mode” is by default set to “Screen Space - Overlay”, but if you want to display a trail renderer, the canvas needs to be able to see it. Having the canvas set to this mode “blocks” it. You can change the canvas “Render Mode” to “Screen Space - Camera” instead, and that should do the trick.

Adding this in case somebody else has the same issue I was having.

I don’t know if this still matters because the question was 2 years ago, but it was the top spot when I googled this issue before finding a solution on my own in Untiy 5.

When changing the order in layer you can change the sorting layer as well. So make a new layer and put it above the “Default” layer on the list. Then mark any objects you want the trail renderer(and object creating the trail) on the “Default” Layer and other objects in the other layer.

PS This is the same way I solved text going behind sprites as well just in case that issue arises as well.