Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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
0
Question by RayJr · Oct 12, 2014 at 03:41 PM · sorting layers

Simple Question: Setting sprite.sortingLayerName

Hello, I can't seem to set sortingLayerName. When I set it, my sprite disappears and ends up with no layer at all. His sorting layer order seems to work fine. Below is my code followed by the console output. Do I need to set it via mask or something? Thanks for the help!!

  • Edit: The problem line, I believe, is 35 **

    using UnityEngine; using System.Collections;

    public class raiselayer : MonoBehaviour {

       public int sortingOrder = 0;
         private SpriteRenderer sprite;
     
         void Start () 
         {
             sprite = GetComponent<SpriteRenderer>();
         }
         
         void Update () 
         {
     
             bool Raise= Input.GetButtonDown Raise
             
             if (Raise)
             {
                 Debug.Log ("Raise called");
                 Raise();
             }
             
         }
     
         void Raise()
         {
     
             Debug.Log ("sprite = " + sprite);
             Debug.Log ("sprite.sortingOrder = " + sprite.sortingOrder);
             Debug.Log ("sprite.sortingLayerName = " + sprite.sortingLayerName);
         
             sprite.sortingOrder = 5;
             sprite.sortingLayerName = "building2";
             
             Debug.Log ("sprite.sortingOrder = " + sprite.sortingOrder);
             Debug.Log ("sprite.sortingLayerName = " + sprite.sortingLayerName);
         }
         
     }
     
    

Console Output:

Raise Called

sprite = MySprite (UnityEngine.SpriteRenderer)

sprite.sortingOrder = 1

sprite.sortingLayerName = building1

sprite.sortingOrder = 5

sprite.sortingLayerName =

Comment
Add comment · Show 4
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 smoggach · Oct 15, 2014 at 07:05 PM 0
Share

try waiting a frame before checking sortingLayerName? It may take a frame for the value to change.

avatar image Eric5h5 · Oct 15, 2014 at 07:21 PM 0
Share

Nothing ever takes a frame to change—all functions are atomic—unless specifically noted in the docs as an async operation.

avatar image RayJr · Oct 15, 2014 at 07:24 PM 0
Share

Thanks for the suggestion. I can see the sprite in the game view sitting on the original layer. Also, if I press "Raise" multiple times, it doesn't change. There is nothing here to set it back to "Building1" where it started.

avatar image MindGem · Aug 08, 2021 at 06:03 PM 0
Share

Hey, bumping this thread.

I'm having the same problem with my prefabs overlapping eachother due to sorting layer (I assume)

I have 1 single prefab, The prefab has different parts, so like Head, Hat, Pants etc.. All of those parts will end up on their separate sorting layer. Problem is, all of the instantiated prefabs with the same part, say the Hat will overlap in runtime..making a mesh, they don't stick to their parents allocated sorting layer. How do I solve this?

I do change my Sorting layer, layer order and even Tags and Layers but Nothing works. I mean, I can see in runtime that it DOES work, they have their new sorting layers and all that but they still overlap and act strange. what the fuck is one suppose to do about that?

 SpriteRenderer rend = GraffitiLetters[i].GetComponent<SpriteRenderer>();
             rend.sortingLayerName = "Letter" + i;
             rend.sortingOrder = 1 + i;


2 Replies

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

Answer by Eric5h5 · Oct 15, 2014 at 07:20 PM

You can only assign sorting layer names that already exist in your project. You need to go into the sorting layer editor and add "building2" manually.

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 RayJr · Oct 15, 2014 at 07:25 PM 0
Share

Hi, Thanks for the suggestion. "building2" is already a sorting layer defined in my game.

avatar image Eric5h5 · Oct 15, 2014 at 08:13 PM 0
Share

It would seem that it's not...make sure the spelling and capitalization match exactly.

avatar image RayJr · Oct 15, 2014 at 09:20 PM 0
Share

Since starting this thread I moved the script from the player object to a trigger collider and re-wrote it all. This time around it worked. I now specify the renderer specifically when calling it and I copied and pasted the name of the layer. Doing all this has worked perfectly. Thanks for all your help.

Here is the final code in case someone is wondering

 public GameObject Inside; // drag in ship hull
 //    public GameObject  Rob;
     private SpriteRenderer sprite;
 
 
 
     void OnTriggerEnter2D (Collider2D bump)
     
     {
 
 
         if (Inside.renderer.enabled == true) {
             Inside.renderer.enabled = false;
             bump.renderer.sortingOrder = 2;
         } else {
             Inside.renderer.enabled = true;
             bump.renderer.sortingLayerName = "boat1" ;
             bump.renderer.sortingOrder = 5;
 
         }
     }

avatar image
0

Answer by DocMcShot · Oct 15, 2014 at 07:16 PM

I believe you just need to reference the renderer component in your code, like so:

 sprite.renderer.sortingOrder = 5;
 sprite.renderer.sortingLayerName = "building2";


Try that and see if it fixes it :)

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 Eric5h5 · Oct 15, 2014 at 07:19 PM 0
Share

The "sprite" variable is already of type SpriteRenderer.

avatar image DocMcShot · Oct 15, 2014 at 07:37 PM 0
Share

@Eric5h5 good call. I didn't read all of the code :/

@RayJr is this script being attached directly to the gameobject that has your sprite renderer on it? Or is the sprite renderer by chance attached to a parent or child gameobject?

avatar image Eric5h5 · Oct 15, 2014 at 08:15 PM 0
Share

That's not the issue...he's assigning the sorting layer name and immediately reading it back and getting nothing, which only happens if the sorting layer name doesn't exist in the project. Hence my answer. It works for building1 but not building2, so I'd guess the actual name in the project is "Building2" or "building 2" or something else that's not "building2". $$anonymous$$ight be nice if Unity would throw an exception for non-existing sorting layer names ins$$anonymous$$d of silently failing.

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

6 People are following this question.

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

Related Questions

How do you get a list of Sorting Layers via scripting? 2 Answers

How to rearrange sorting layers via script? 1 Answer

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

How to change sorting layer of material/3D object so it appears in front of the sprites? 0 Answers

question about sorting layer in a topdown 2D,Hi! 2 Answers


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