- Home /
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.
Answer by Commoble · Mar 07, 2017 at 07:03 AM
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.
Yeah, I thought about that. It just seems like there would be a faster, simpler way.
Thanks for the help though.
Your answer
Follow this Question
Related Questions
Sorting Layer Logic Issue 0 Answers
How do I set the rendering order of objects with the same sorting layer and order? 1 Answer
Sprites rendered in the wrong order 1 Answer
5.3 : Display particles over an Image component 1 Answer
UI object drawing order is different between Unity Editor and Standalone version 1 Answer