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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to control sprite SortingLayerID via c#?

Discussion in '2D' started by Arthurtnt, Mar 9, 2016.

  1. Arthurtnt

    Arthurtnt

    Joined:
    Dec 2, 2012
    Posts:
    6
    Hi!

    I want my sprite to get its parent SortingLayerID.
    But I don´t know how to access this value. It doesn´t seem to be accessible.
    Is it possible to be changed?

    Thanks!
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,848
    It's a property of the Renderer class; see here.
     
  3. Arthurtnt

    Arthurtnt

    Joined:
    Dec 2, 2012
    Posts:
    6
    Thank´s Joe!
    Actually, I just figured it out!

    this is the code:
    Code (CSharp):
    1. this.gameObject.GetComponent<SpriteRenderer> ().sortingLayerID = this.gameObject.transform.parent.GetComponent<SpriteRenderer> ().sortingLayerID;
     
    JoeStrout likes this.