Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
7
Question by negative_zero · Jan 08, 2014 at 06:30 AM · c#2d-platformersprite renderer

How do you change a sprite's sorting layer in C#?

Basically I am making a 2D platformer and I have a sprite that is in the foreground and when my player flips a switch, I want the sprite to move to the background. I can do this manually by changing the Sprite Renderer sorting layer, but I can't seem to figure out how to change this in the code, or if it is even possible?

If not, what would be an alternate solution for what I'm trying to do?

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
17

Answer by NoseKills · Mar 13, 2014 at 01:57 PM

To add the layer changing to emalbs answer since his example only changed sorting order on the same layer

 using UnityEngine;
     using System.Collections;
 
 
     public class SortingOrderScript : MonoBehaviour
     {
         public const string LAYER_NAME = "TopLayer";
         public int sortingOrder = 0;
         private SpriteRenderer sprite;
 
         void Start()
         {
            sprite = GetComponent<SpriteRenderer>();
 
            if (sprite)
            {
              sprite.sortingOrder = sortingOrder;
              sprite.sortingLayerName = LAYER_NAME;
            }
         }
     }
Comment
Add comment · Show 2 · 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 rollrodrig · Sep 06, 2014 at 08:14 PM 0
Share

It works for me, i dont know why this cain of samples are not in the unity docs... Thank you again

avatar image Mr_yilanci · Apr 02, 2021 at 09:28 PM 0
Share

Thanks man ... it working :)

avatar image
7

Answer by emalb · Jan 08, 2014 at 01:47 PM

Have a go at setting your SpriteRenderer's sortingOrder. The following example should enable you to manipulate it in the Inspector as your game runs.

 using UnityEngine;
 using System.Collections;
 
 public class SortingOrderScript : MonoBehaviour
 {
     public int sortingOrder = 0;
     private SpriteRenderer sprite;
 
     void Start()
     {
         sprite = GetComponent<SpriteRenderer>();
     }
 
     void Update()
     {
         if (sprite)
             sprite.sortingOrder = sortingOrder;
     }
 }
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 Simon Says · May 15, 2014 at 03:11 PM 0
Share

Just WTF is this not in the documentation?! Thanks for the info, guys. Sorting layers are defined for all Renderers (in the API), if anyone wonders.

avatar image
0

Answer by vickygroups · Apr 17, 2014 at 09:42 PM

thank you, NoseKills - I used this, too!

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
0

Answer by rollrodrig · Sep 06, 2014 at 10:28 PM

Thank you very much !! I was looking for it a lot !!

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
0

Answer by koslovdenis · Jun 12, 2017 at 07:09 PM

Heyy guys what about multiple sprites at the same time?

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 ShadyProductions · Jun 12, 2017 at 07:08 PM 0
Share
             SpriteRenderer[] sprites; //fill this however fits your game
             foreach (var sprite in sprites)
             {
               sprite.sortingOrder = sortingOrder;
               sprite.sortingLayerName = LAYER_NA$$anonymous$$E;
             }
  • 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

29 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Delayed response: Jumping up from ledge 0 Answers

2d c# boss follow code 0 Answers

Can't move instantiated 2d game oject. 0 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