Can "Sorting Group" be accessed from script?

So you can enable/disable or change the layer during runtime?

3 Likes

Yep. Sorting layer and sorting order are part of the Renderer class:

https://docs.unity3d.com/ScriptReference/Renderer-sortingLayerName.html
https://docs.unity3d.com/ScriptReference/Renderer-sortingOrder.html

So you could do something like this:

GetComponent<Renderer>().sortingLayerName = "Default";
GetComponent<Renderer>().sortingOrder = 0;
2 Likes

No sorry, I’m referring to the new component in Unity 5.6 called “Sorting Group”.

1 Like

Ah sorry, my mistake!

Have you tried

GetComponent<SortingGroup>().sortingLayerName = "Default";

Looks like it should work.

https://docs.unity3d.com/ScriptReference/Rendering.SortingGroup.html

2 Likes

I was thinking that too but it seems “SortingGroup” isn’t a type or maybe I’m missing a namespace or something.

1 Like

UnityEngine.Rendering

13 Likes

These are the kinds of things that should be explicitly called out in the Unity API docs. I know it lists its inheritance, but a simple example on how to call this component would be extremely helpful.

8 Likes

Thank you, ive been looking for where that was. Thought it might be in their documentation but it didnt specifically mention.

1 Like

Just for future reference since this is an old post being unearthed:

You can indeed find the namespace in the doc header:

and while it’s not always a great idea to rely on your IDE for everything, it’s in your best interest to be using one that can tell you the namespace you’re missing.

3 Likes

correct answer:
Just look at the reference code.
To use SortingGroup, you must add UnityEngine.Rendering name space.

first:
using UnityEngine.Rendering;
Then:
GetComponent<SortingGroup>().sortingOrder = 1;

Good luck

3 Likes

Thanks this post was really useful!