Cagus
Cagus

Reputation: 33

Unity - sorting layers intertwined for objects from the same prefab

I am new to unity and some concepts still eludes me.

I made an object with children (card) and made prefab, then I made variants and put them on the scene. Unfortunately the cards get messed up like on the picture. The children are in Default sorting layer and everyone of them has iterated number 1,2,3,4...

What am I making wrong if I want one card be over the second as separated object (with shadow and everything) and I intend to spam a lot of them (so I want to evade manually add or increase Z for each of them).

Also, it is not UI.

Image describing the problem

Thanks for help in advance

Upvotes: 3

Views: 2239

Answers (1)

Joseph Horsch
Joseph Horsch

Reputation: 564

You can simply add a "Sorting Group" component to your Card prefab.

  • In the prefab view, for your Card, press "Add Component" in the inspector.
  • Search for "Sorting Group" and click to add.

You should also create a new "Sorting Layer" for your Card.

  • Click the dropdown and select "Add Sorting Layer".
  • Name it "Card Layer"
  • Add the "Sorting Layer" to the "Softing Group" as well as all child sprites in your Card.
  • You can use "Order in Layer" to order children objects in your new "Card Layer"

Reference: https://docs.unity3d.com/Manual/class-SortingGroup.html

If you are looking to have dynamic text, "TEST TEST" or "5", for example, this becomes a little more complicated. I have had a lot of ordering issues using Prefabs with sprites and canvas/UI objects. I get unexpected behavior with "Sorting Group" in that scenario. I was able to solve this by adding a "Mesh Render" object with a "Text Mesh" component instead.

Upvotes: 3

Related Questions