Change sprite's sorting layer?

Hi, I’ve got a small list of sorting layers for all my sprites (“background”, “midground”, “foreground”, etc.), and I’m trying to change it for a few sprites through a c# script.

I’ve looked around, but only things I found were Unity Answers questions from 2014, and I don’t know whether they were relating to the old GUI system, or the new UI one.

I tried typing “Sprite.” and “SpriteRenderer.” for the sprite with the script attached, and I went though anything I could think of, like “.sortingOrder”, “.sortingLayer”, etc. But nothing was coming up in the auto completion of MonoDevelop.

So how do I change the sorting layer?

This works for me.

this.GetComponent<SpriteRenderer>().

auto complete in Visual Studio gives me access to sortingLayerID, sortingLayerName and sortingOrder.

If this is how you’re accessing it, then maybe it’s an issue with MonoDevelop.

EDIT: I also found this in Unity Answers How do you change a sprite's sorting layer in C#? - Questions & Answers - Unity Discussions

1 Like

Yeah, it’s an issue with MonoDevelop.

Thanks. :slight_smile: