I’m using Unity 2019.4 LTS.
I have two sprites under a parent gameobject (the parent has a sprite renderer too) in Hierarchy, their sprite renders have the same configurations, sorting layer: Default, order in Layer: 0, they have the same tag and are in the same layer. The texture type of them is the same: Sprite (2D and UI).
Earlier, they are rendered in order as expected, the sprite A is on top of sprite B in hierarchy, and the B is rendered later than A, that B in game covers B.
But later on, all of a sudden, no matter how I switch their order in hierarchy, A always covers B in scene view and game view.
Although they are rendered in the right order if I set them to different order in layer, but the order in Hierarchy doesn’t make any difference now.
Did I accidentally click something that makes the change? This causes some trouble in my project.
PS: Pivot of sprite B is changed in sprite editor, but right after this, everything works fine, so I don’t think this is the cause.
Thanks for the reply. and nothing’s wrong with that. But firstly, I just wanted to know why it worked as intended earlier in the project, but not later all of a sudden, hoping to find out a potential problem I accidentally made, so I won’t make similar problem in the future that could cause more trouble. Secondly, some logic in the scripts is dependent on the intended way of setting the order of rendering I mentioned earlier, I hope I don’t need to change the scripts.
Unfortunately, Im not sure about if the hierarchy has anything to do with renderering order. Im also not sure why it would switch all of a sudden. I would just use the Order in Layer on the sprite to designate those. You shouldnt have to change any scripts, but a benefit of using the Order in Layer is you can access that within a script and change them at runtime in case you need to.
hierarchy is not supposed to be used for sorting. When you have 2 sprites occupying the same order you are lucky it doesn’t just glitch out as it does in any 3d software when you overlap 2 planes. You have to decide which sprite you want to render over the other and the tools to do that are either order in layer, order by z axis, or sorting groups.
The hierarchy working one way or the other is just random, unity takes it as you having not made a decision on how you want things ordered
Assuming the array returned by GetComponentsInChildren is ordered by hierarchy, a custom script to add this sort of behavior should be fairly easy to add.
Rough example:
//You would add this to the root object containing all the child SpriteRenderers you want to sort.
public class SpriteHierarchySorter : MonoBehaviour {
private SpriteRenderer[] childRenderers;
private void Awake() {
childRenderers = GetComponentsInChildren<SpriteRenderer>();
//The farther down the hierarchy, the higher the sorting order is.
for(int i = 0; i < childRenderers.Length; i++) {
childRenderers[i].sortingOrder = i;
}
}
}
The default 3D Renderers draw stuff according to Z depth - distance from camera.
SpriteRenderers draw according to their Sorting Layer and Sorting Depth properties
UI Canvas Renderers draw in linear transform sequence, like a stack of papers
If you find that you need to mix and match items using these different ways of rendering, and have them appear in ways they are not initially designed for, you need to:
identify what you are using
search online for the combination of things you are doing and how to to achieve what you want.