2D Sprite always behind background

I am creating an 2D android game. I got a helicopter sprite and added as a 2D sprite however, it always goes behind my background for some reason. Here is a screenshot:

why is this happening?

When using Sprites, you must change the Z Position to change who will appear in front of who.

If you want do sort by hierarchy, you must use Image and not Sprite (but Image is better for UI, so use the Z order) =).

12 Likes

Ideally Z ordering is the last thing to determine what is in front of what. Typically in a 2D game you almost never have to worry about Z if you do things right.

You have order in hierarchy for the canvas, as well as Sorting Layer and Sorting Order on every Sprite Renderer and Canvas.

Try creating more sorting layers Background, Midground, Foreground, etc, and assigning the objects to the correct layers. Sorting Order is used to determine order within a layer.

Edited (2018): Now you also have SortingGroup, which makes everything underneath it in the hierarchy sort locally, and the entire SortingGroup is then sorted together preserving that local sorting among other Sprites and SortingGroups.

Alternatively, you can use the TransparencySortMode in the settings to set a custom axis for sorting, which for instance could be set to sort on the Y axis. See this for an explanation of that technique:

8 Likes

I created these layers:

I assigned each object to a layer:


however I am still getting the same results. Am I doing it right?

1 Like

Yes, if the background is a UI Image and still in front of the helicopter, check your Canvas settings for Sorting Layer and Sorting Order.

Also, you don’t have to keep Layers and Sorting Layers identical. For render-order, Sorting Layer & Order is what matters. The other Layers is for grouping objects for things like Collision interactions, raycasts, use with LayerMasks, etc.

5 Likes

I set the Render Mode on the canvas to World Space and it seems to be working now. Thanks :slight_smile:

3 Likes

No problem. One thing to keep in mind is that Screen Space - Overlay will render above everything (probably what you were seeing). Camera Space has Sorting order and renders relative to the camera bounds. World space canvas exists in the 3D world relative to the canvas in world coordinates.

5 Likes

if you still facing this problem then in the transform pane into the position pane put any negative value of z then your selected sprite will appear in front of your background

viz i have selected z = -0.01 and it will really work

5 Likes

i love u

2 Likes

Hello, glad this got fixed. I also had issues with my 2D game between sprites and backgrounds that were on meshes. If it helps anyone who lands on this thread, here is a tiny script I wrote that you can attach to any gameobject that has a mesh renderer - it lets you easily set the sorting order which will line up with Sprite sorting orders. Note it only uses the Default layer, but you can easily change that by making a public string of the layer and putting it in place of “default”, then setting it in the inspector.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MeshRendererSortOrder : MonoBehaviour {

public int sortOrder;

// Use this for initialization
void Start () {
MeshRenderer mr = GetComponent ();
mr.sortingLayerName = “Default”;
mr.sortingOrder = sortOrder;
}

}

1 Like

This worked! Thanks

2 Likes

Sorting layers really

Hey, it is also about your Canvas sequence: I have spent much time to solve it, unfortunately :slight_smile: . I mean; If you add the dark green image to your hierarchy canvas before light green image, the dark green image will always be behind of light green, and vice versa

I fixed this by changing the ‘Sorting Layer’ property on the ‘Sprite Renderer’.

nope. this doesn’t work unless you 've left out alot of other issues

Let’s close this thread, it’s just being necro’d without anything useful being added.

If you have an issue, create your own thread, state what you’ve tried etc. Just posting a single-liner on an old thread is lazy posting and won’t get your problem solved.

Thanks.