# Issue with Sorting Layers with an NPC and Tilemap

I have a weird thing happening, that I can't quite explain. I have two scenes, my starting town, and an Arcanist's shop. When I go into the Arcanist's Shop, the Arcanist NPC stays on the same sorting layer (background layer 0) as my checkered tiles, and the table is background layer 1. When I start the game, the NPC will switch between being "behind" the table, or on top of it whenever I reenter the Arcanist's Shop scene. It seems to switch between background layer 0 and 1 with no cause.

I've double checked my sorting layers, they're all where they should be. I've checked the camera's culling mask, it's fine. I've checked to see if there are any weird components on anything in my shop scene, and there aren't

See the image attached, this is the same instance of the game, just different times when I enter the Arcanist's Shop.

Screenshot of my tilemap setup:

• How have you configured the sorting behaviour of your tilemap? Have you combined all your sprites onto one atlas texture? Apr 12 '20 at 11:09
• @DMGregory, thanks for responding. If I understand your tilemap question properly, it goes like this: Two layers, a background, and an objects layer. The background is on the background layer 0, and objects is background layer 1. I've checked my layers settings in the inspector, and it goes, from lowest priority to highest: default->background->player. Apr 12 '20 at 22:31
• @DMGregory As for your atlas texture question, I've never heard of an atlas texture, and I'm unsure of what it is when I googled it. Apr 12 '20 at 22:32
• Just show us a screenshot of how your tilemap is configured in your inspector, please. Apr 12 '20 at 22:37
• @DMGregory I've uploaded one, I'm sorry if I didn't give the correct information. Apr 12 '20 at 22:43

I see you're using the default "Chunk" draw mode for your tilemaps. As noted in the documentation for Tilemap sorting:

When set to Chunk Mode, the Tilemap Renderer handles Sprites on a Tilemap in batches and renders them together. They are treated as a single sort item when sorted in the 2D Transparent Queue. This reduces the number of draw calls to improve overall performance, however other Renderers cannot be rendered in between any portion of the Tilemap which prevents other rendered Sprites being able to interweave with the Tilemap Sprites.

So this could account for the behaviour you're seeing. Depending on an arbitrary decision made by the chunking logic when your scene is loaded, the table might get built into a chunk that's wholly in front of, or wholly behind your NPC, with no ability for the NPC to get wedged in-between these elements.

So, you can instead set your objects tilemap layer to "Individual" draw mode - at the cost of more draw calls, but more fine-grained sorting behaviour.

Then be sure to set your sorting behaviour: Edit > Project Settings > Graphics > Transparency Sort Axis

Set it to Custom Axis, with a positive y, so that sprites higher up on your screen render behind objects lower down. (Here I also left the z-component set, so I can also fudge the sorting order by moving objects on the z-axis where needed)

With the individual draw mode and custom sort axis set, your NPC will be drawn behind tiles that are lower than them, and in front of tiles that are higher than them on the screen (you can choose in the sprite settings whether to sort based on center point or the sprite pivot for even more control)

• You are a legend! Thanks so much for the help! It's working now thanks to everything you've suggested. Apr 19 '20 at 23:30