How to Spawn a Sprite that is always in front or on Top of Another Sprite?

Right now I’m spawning random sprites n the screen. Sometimes they spawn on top of each other, sometimes they spawn behind each other.

How do you spawn the sprites so that they are always on top f each other? So that the most recent spawned sprite appears in front of a previously spawned sprite?

Something abut sorting layers?

Thanks.

Normally, different sorting layers are good for deciding the order that sprites appear in, but if you’re making an arbitrarily large number of sprites, you’ll probably want to set the order in layer instead. You can do this by getting a reference to the object’s sprite renderer when you instantiate it, and setting spriteRenderer.sortingOrder.

Each time you spawn a new object, set the sorting order to one higher than the previous one. The sorting order only goes up to 32767, so you’d want to reset the counter periodically.