Order in Layer value equal to Y Axis Position

Hi,
Is there anyway that you can make it so that the “Order in Layer” value of a Sprite Renderer Component is equal to the Y Axis of the game object it is attached to? I want to do this because I am creating a 2D Top-Down RPG type game where I want it to look like the higher on the screen an object is, the further back it is.

Many Thanks,

Tommy Eaves

First, you’ll need to set up a reference to your sprite renderer component…

private SpriteRenderer spriteRend;

void Start(){
    spriteRend = GetComponent<SpriteRenderer>();
}

Then where you want to change the Order in Layer value…

spriteRend.sortingOrder = insertYPositionVariableHere;

Have fun :smiley: