Sorting layers according to Y axis

Hello - I am new to Unity, and I am trying to make a 2D, top-down, isometrical game. I am wondering how I can sort the layer of objects based on their position of the Y axis using a script. In other words, I am trying to create the effect of a background/foreground according to Y position. For example, if object_1 is beneath object_2, the layer for object_1 should be greater than the layer for object_2.

If anyone can help me, I would greatly appreciate it. Thanks!

###Update for 2020:

Use Transparency Sort Mode on a Camera. You can conveniently set a custom axis project wide to sort based on Y axis instead of Z, mix with Sorting Groups to keep a whole character made of multiple sprites together and sort as a single entity.


Not to necropost, but since this is the first search result on Google & Bing, if anyone in is here in 2019, there is a much cleaner solution.

Edit > Project Settings > Graphics > Camera Settings > Transparency Sort Mode > Custom Axis
… > Transparency Sort Axis > (0, 1, 0)

If you are using Universal Render Pipeline you will not see Camera settings in Graphics menu

Instead, in Assets create new asset of type Renderer 2D Data (Create/Rendering/Universal Render Pipeline/2D Renderer), configure transparency in it’s inspector and then use it as a default renderer in yout URP object.

I can’t believe how easy it that was to implement. Thank you, @Broxxar, this worked perfectly for me.