Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved Weird thing happening with sorting layers

Discussion in '2D' started by Sekudo, Jun 26, 2023.

  1. Sekudo

    Sekudo

    Joined:
    Dec 4, 2022
    Posts:
    36
    I have a sorting layer on one gameObject and it is supposed to be in front of another object, gameObject 2, and it works. However, gameObject 2 has a costume switching function, and after it switches, it goes in front of gameObject 1 DESPITE THE SORTING LAYERS. How do I solve this? I've tried sorting groups putting them on the same layer and then just using order in layer, nothing seems to work. Thank you for reading.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    Three (3) primary ways that Unity draws / stacks / sorts / layers / overlays stuff:

    https://forum.unity.com/threads/orthographic-camera-rendering-order.1114078/#post-7167037

    In short, as far as the Standard Rendering Pipeline,

    1. The default 3D Renderers draw stuff according to Z depth - distance from camera.

    2. SpriteRenderers draw according to their Sorting Layer and Sorting Depth properties

    3. 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:

    https://docs.unity3d.com/Manual/2DSorting.html

    And SortingGroups can also be extremely helpful in certain circumstances:

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

    Other rendering pipelines may have other ways of layering (HDRP and URP). Go see the latest docs for details.
     
  3. Xiangting_Su

    Xiangting_Su

    Unity Technologies

    Joined:
    Sep 22, 2020
    Posts:
    246
    Yeah, it does sound strange. Do you have a screenshot of the Scene and the Inspector. Just so it's easier for us to identify the hitch.

    Curious also, does your costume switching function assign a Sorting Layer and Order in Layer when switching between costumes?
     
  4. Sekudo

    Sekudo

    Joined:
    Dec 4, 2022
    Posts:
    36
    I got it fixed. I apologize for taking up your time:(