game objects on the same sort layer and sort order changing positions

I’m spawning game objects which are on the same sort layer and 0 sort order, and I’ve noticed that sometimes as they are passing each other, the object sorted underneath the passing object will suddenly be on top. What is causing this? If I spawn X number of objects on the same sort layer and sort order, doesn’t unity automatically sort them based on which spawned first?

Sprites on the same layer and order in layer will sort based on the current Transparency Sort Mode and Axis, which by default is camera depth i believe, the Z axis.

1 Like

So how do you prevent sprites in the same layer randomly changing sort position while they are overlapping each other?

Give them different order in layers, or different Z positions. Although the sorting does not change randomly, something in your system is changing to affect the order they are being rendered.

Make sure you’re using orthographic transparency sort mode, it’s in your ProjectSettings > Graphics menu.

You can also set this per camera in code: camera.transparencySortMode = TransparencySortMode.Orthographic

1 Like