I’m making a card game and all of the cards are copies of a gameobject made using Instantiate.
The cards have a sprite renderer, box collider and script.
The cards also have some children gameobjects with just a sprite renderer. These are used for icons, so that if I want to display an icon on a given card the card’s script will set the child to active, move it into the right position on the card and change its sprite image to the required icon.
What I would like is for the icon spriterenderer to always be rendered after the card it is an icon for, but not after all cards.
At first I had the card gameobjects and child gameobjects on the same sorting layer and order in layer, with the child having a z-position of -0.01 (relative to the card, so it should always appear above it)
This does not work and I can not figure out why. Obviously the cards move in play, which affects their position, but pausing it and consulting the child objects still have a negative z-position and are still children of the other objects and are still on the same layer, so I cannot fathom why they are not rendered above them.
I can make the icons render after the card using the sorting layer, but if I do that they render after all cards, which means that if one card passes above another during an animation all of the icons on the lower card show “through” the card above.
I have found a few posts by other users with similar issues, but they are generally trying to make A in front of B without the restriction that an A should not be in front of an instantiated copy of B with a different z-position, so those solutions did not work for me.
Three (3) primary ways that Unity draws / stacks / sorts / layers / overlays stuff:
In short, as far as the Standard Rendering Pipeline,
The default 3D Renderers draw stuff according to Z depth - distance from camera.
SpriteRenderers draw according to their Sorting Layer and Sorting Depth properties
UI Canvas Renderers draw in linear transform sequence, like a stack of papers
If you find that you need to mix and match items using these different ways of rendering, and have them appear in ways they are not initially designed for, you need to:
identify what you are using
search online for the combination of things you are doing and how to to achieve what you want.
There may be more than one solution to try.
For instance, you may even use multiple co-located cameras to more-explicitly control apparent draw ordering.
Additional reading in the official docs:
And SortingGroups can also be extremely helpful in certain circumstances:
Other rendering pipelines may have other ways of layering (HDRP and URP). Go see the latest docs for details.
There’s lots of third party open source packages available as well, such as this:
Hi, I had a similar problem and the solution to this is the Sorting Group component. You have to drag this to the top game object of your card object. There you can now apply the settings of your map sprite. You can then adjust the sorting layer and sorting order for all sprites within the game object as you wish, without it having any effect on other game objects.
I hope I could help you with this!
best regards
Simon