Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
5
Question by GiyomuGames · Sep 25, 2015 at 07:42 AM · 2dspriterenderermeshrenderersorting layers

Unity 5: MeshRenderer and SortingLayer not working?

Hello!

I am creating a 2D platformer, I have MeshRenderers (for the tiles) and SpriteRenderers (single objects) co-existing.

For the sprites of course the sorting layers and sorting orders work perfectly. For the MeshRenderers I am able to set their sorting layers and sorting orders in a script as many people suggest, however they are still drawn behind all my sprites regardless of the sorting layer I put them in. It seems that this was working fine until Unity 5 but is not working anymore.

Does anyone know how to make it work or what I am doing wrong?

P.S.: I have put Zwrite Off in my shader as I believe this is the right thing to do when using sorting layers instead of Z ordering. Additionally, if I put Zwrite On then my meshes are drawn on top of all my sprites.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

6 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by GiyomuGames · Oct 13, 2015 at 11:23 PM

Edit: I have got the definite answer thanks to Unity QA team. This is what they told me to do and it worked perfectly:

To solve this issue set Inspector to debug mode (right click on inspector tab and select debug). After doing that select "Tiles Normal Map" and "Sprites Normal Map" and change their Custom Render Queue so that both would be the same.

"Tiles Normal Map" being the material I use for my tiles using a MeshRenderer and "Sprites Normal Map" the material I use for my regular sprites. They both use the same shader but a different texture and normal map. When I checked, indeed they didn't have the same render queue which explains why the ordering wasn't working at all.

Thanks Unity QA!


Hello there.

I haven't found a way to make the MeshRenderers work with the Sprites and the sorting layers. Therefore I have used a work around which consists in using different cameras with different depths to ensure a correct drawing order.

I am still interested in a way to sort all that without using several cameras so don't hesitate to reply later.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Pharan · Nov 23, 2015 at 04:22 PM 0
Share

When you say you're setting it "in a script", how exactly are you doing that? Can you show the exact code and logic that does that?

I use Sorting Layers via script all the time and I've not experienced any problems. But I never set them by name. I just copy values from a SpriteRenderer.

avatar image GiyomuGames Pharan · Nov 23, 2015 at 11:20 PM 1
Share

Hi Pharan,

I am simply doing:

     my$$anonymous$$eshRenderer.sortingLayerName = "Platforms";
     my$$anonymous$$eshRenderer.sortingOrder = 0;

I have also a plugin which makes this visible in the editor and therefore I can confirm that the sorting layer name and order have been set correctly.

avatar image eriknastesjo · Feb 01, 2020 at 04:24 PM 1
Share

I think I have the same issue but I can't for the life of me find "Tiles Normal $$anonymous$$ap" and "Sprites Normal $$anonymous$$ap"??alt text

untitled.png (180.7 kB)
avatar image
4

Answer by gapMindful · Mar 21, 2016 at 09:45 AM

Changing the rendering mode to either transparent or fade fixed things for me. Same issue, different root cause I suppose

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

Answer by Cherno · Oct 14, 2015 at 09:04 AM

Try this script on the object:

 using UnityEngine;
 
 [ExecuteInEditMode]
 public class SetSortingLayer : MonoBehaviour {
     public Renderer MyRenderer;
     public string MySortingLayer;
     public int MySortingOrderInLayer;
     
     // Use this for initialization
     void Start () {
         if (MyRenderer == null) {
             MyRenderer = this.GetComponent<Renderer>();
         }
             
 
         SetLayer();
     }
 
 
     public void SetLayer() {
         if (MyRenderer == null) {
             MyRenderer = this.GetComponent<Renderer>();
         }
             
         MyRenderer.sortingLayerName = MySortingLayer;
         MyRenderer.sortingOrder = MySortingOrderInLayer;
         
         //Debug.Log(MyRenderer.sortingLayerName + " " + MyRenderer.sortingOrder);
     }
  
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image GiyomuGames · Nov 10, 2015 at 01:02 AM 0
Share

Hi Cherno, sorry I just saw your comment! I am pretty sure I tried that. Do you confirm that it works for you with Unity 5 and above?

avatar image Cereal_Killa · Jan 25, 2017 at 05:08 AM 0
Share

This sets the order in layer in the editor but doesn't stop all z-fighting. If I move the dark brown layer to the same layer as the green it will overlap it completely, but when it's back 1 layer it draws mostly behind except for 2 lines which stay in front. This is quite strange as it's the middle of the brown quad - there is no edge there - I can't figure it out.

As you can see by the z-fighting in the editor the brown quad and green quads are set to the same z transform value (0), while the light green and black are both z=1, and they both clip in the same way.

As the camera moves up and down the lines flicker terribly. The brown and black layers are both layer 13 while the green layers are both layer 14 which should always render in front.

alt text

Changing all layers to transparent fixes the problem and also allows layers that are further back in z order to overlap those in front as well which could make for some interesting effects.. I'm guessing turning z-write off for all shaders would give the same effect.

avatar image yoavhops · Sep 04, 2018 at 11:04 AM 0
Share

That fixed the issue for me, didn't know there is a sorting layer for a render that is not spriteRenderer.. was looking for a fix for that issue for a long time, so thanks a lot.

avatar image
2

Answer by tucano · Jan 04, 2017 at 11:25 AM

I use the following material with Render Queue Transparent:

Material

And a script to set sortingLayer and Order:

 myMeshRenderer.sortingLayerName = "Platforms";
 myMeshRenderer.sortingOrder = 0;

screen-shot-2017-01-04-at-120122.png (22.9 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

Answer by WMorais · Apr 30, 2019 at 09:15 PM

Awesome workaround Custom Editor to Expose Sorting Layers

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DavidSof · Jul 01, 2021 at 11:49 AM 0
Share

tried it on 2021.1.3f1 and no results ,just put it in a simple two cubes with simple camera

  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

39 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

[2D] [C#] Sprite disappears after changing position 1 Answer

Need more flexible sorting layers 1 Answer

Render particles in front of any sort layer (2D sprites) 3 Answers

Render mesh in front of another mesh but not in general 2 Answers

Is it possible to use the unity standard shader with a sprite renderer to add normal maps to sprites, without needing a new material for each sprite sheet? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges