• 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
    • Ask a question
    • Spaces
    • Home /
    avatar image
    5
    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?

    Add comment

    5 Replies

    · Add your reply
    • Sort: 
    avatar image
    10

    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

    1. using UnityEngine;
    2. using System.Collections;
    3. public class SortingOrderScript : MonoBehaviour
    4. {
    5. public const string LAYER_NAME = "TopLayer";
    6. public int sortingOrder = 0;
    7. private SpriteRenderer sprite;
    8. void Start()
    9. {
    10. sprite = GetComponent<SpriteRenderer>();
    11. if (sprite)
    12. {
    13. sprite.sortingOrder = sortingOrder;
    14. sprite.sortingLayerName = LAYER_NAME;
    15. }
    16. }
    17. }
    Add comment · Hide 1 · Share
    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
    3

    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.

    1. using UnityEngine;
    2. using System.Collections;
    3. public class SortingOrderScript : MonoBehaviour
    4. {
    5. public int sortingOrder = 0;
    6. private SpriteRenderer sprite;
    7. void Start()
    8. {
    9. sprite = GetComponent<SpriteRenderer>();
    10. }
    11. void Update()
    12. {
    13. if (sprite)
    14. sprite.sortingOrder = sortingOrder;
    15. }
    16. }
    Add comment · Hide 1 · Share
    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!

    Add comment · Share
    avatar image
    0

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

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

    Add comment · Share
    avatar image
    0

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

    Heyy guys what about multiple sprites at the same time?

    Add comment · Hide 1 · Share
    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_NAME;
                 }
    

    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.

    Welcome to Unity Answers

    The best place to ask and answer questions about development with Unity.

    To help users navigate the site we have posted a site navigation guide.

    If you are a new user to Unity Answers, check out our FAQ for more information.

    Make sure to check out our Knowledge Base for commonly asked Unity questions.

    If you are a moderator, see our Moderator Guidelines page.

    We are making improvements to UA, see the list of changes.



    Follow this Question

    Answers Answers and Comments

    28 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

    Related Questions

    Multiple Cars not working 1 Answer

    Distribute terrain in zones 3 Answers

    Private Field is assigned but its value is never used, but i use it 1 Answer

    How to check if an object hits the ground hard enough then add explosive force around it (2D) 1 Answer

    Can someone help me make an Enemy AI script? 3 Answers