Why doesn’t my particle system show in front of my sprites??

Particle System Sort LayerThis one had me befuddled for a little while. I could not figure out why my particle system was not showing up in front of my sprites for the life of me. I had the “Layer” set properly but I could not find a place to set the “Sorting Layer”. I came across some other folks who were saying you had to set the sorting layer manually in code so the particle system would show up where it needed to (sorry, I can’t remember the references!). Okay, that seems easy enough, let’s see if we can figure that out. It looks like we can do that via the following code. The “sortingLayerName” is just a string of a sorting layer you have defined in Unity and the “sortingOrder” is an integer that specifies the z-order in that sorting layer. The higher the number, the closer the object “looks” to you.


particleSystem.renderer.sortingLayerName = sortingLayerName;
particleSystem.renderer.sortingOrder = sortingOrder;

So now let’s just make this into a script so we can easily apply it to all particle systems we create.

I put the following code into a file named “ParticleSystemFix.cs” and attached it to any particle systems I create.

using UnityEngine;
using System.Collections;

public class ParticleSystemFix : MonoBehaviour {

public string sortingLayerName;
public int sortingOrder;

/**
 */
void Start() {
    particleSystem.renderer.sortingLayerName = sortingLayerName;
    particleSystem.renderer.sortingOrder = sortingOrder;
    enabled = false;
  }
}

I’ll usually tell a script to disable itself once it is done initializing or if the code in the Update() function starts to be skipped because of a certain condition. I’m sure there has to be a slight performance advantage to doing this…every little bit helps.  Plus it lets me know if something is executing properly when I expect the script to become disabled.

Particle System Sort LayerNow, just attach this script to any particle system; there in the inspector, you can define a “sortingLayerName” and “sortingOrder” on the “ParticleSystemFix”.  I used a large number like 999 as my “sortingOrder” since I want my particle systems to appear on top of all my sprites.

 

 

Particle System Sort LayerMake sure that what you pass into the “sortingLayerName” is defined in Unity!

That is about it, as long as the “sortingLayerName” for your particle system is where it needs to be hierarchically, your particle system should appear on top of the sprites.

 

 

 

 

Comments are closed.