Search Unity

gameObject.GetComponent<SpriteRenderer>().sortingOrder always returns 0

Discussion in 'Scripting' started by egg_citizen, Jul 26, 2019.

  1. egg_citizen

    egg_citizen

    Joined:
    Jan 3, 2019
    Posts:
    19
    Hello all,

    probably I don't understand the function correctly, but I wanted to get the order of layers from the gameobjects in my scene. This, I want to later use, to drop another object just underneath the object I hover over. (But that's not the question for now)

    I use the following code:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. namespace HideSeek.Core
    4. {
    5.  
    6.     [RequireComponent(typeof(Sprite))]
    7.  
    8.     public class ObjectInScene : MonoBehaviour
    9. {
    10.         private int sortingOrder;
    11.  
    12.         private void Start()
    13.         {
    14.             Debug.Log("Hello, I'm " + gameObject.name + " and my sortinglayer is: " + gameObject.GetComponent<SpriteRenderer>().sortingLayerName + ". But is my layerorder: " + gameObject.GetComponent<SpriteRenderer>().sortingOrder + "?");
    15.         }
    When running the code, I get the correct name of the objects, the correct name of the layername... but layerorder is always 0. I thought this should get me the sortinglayer orders the individual gameobjects where connected to. (there are about 10 sorting layers, the gameobjects are connected to different SortingLayers)

    So:
    1. Do I understand the function correctly and it doesn't work? (and how do I get it to work?)
    or, probably:
    2. I don't understand the .sortingOrder function. So what does it mean and how DO I get the sorting layer? Or won't I need the sortinglayerorder to set another gameobject underneath a Gameobject?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    I love this type of testing. It's how you get to the bottom of stuff.

    So what I hear you saying above is that you set the SpriteRenderer sortingOrder to a non-zero number in the inspector, you see it as a non-zero number in the inspector, and yet when you run the above code on that SpriteRenderer it actually returns zero?? That's ... interesting. Is that really what is going on?

    ALSO, I think what you want is this:

    Code (csharp):
    1. [RequireComponent(typeof(SpriteRenderer))]
    (you have require "Sprite")
     
    egg_citizen likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Followup: I ran your script with one sprite, setting its SortingLayer and Order In Layer to not-default values, and they came back as expected:

    Hello, I'm AirDay_FG and my sortinglayer is: TopLayer. But is my layerorder: 4?
    UnityEngine.Debug:Log(Object)
    xxx:Start() (at Assets/xxx.cs:12)
     
    egg_citizen likes this.
  4. egg_citizen

    egg_citizen

    Joined:
    Jan 3, 2019
    Posts:
    19
    Hello Kurt-Dekker, thank you very much for your input.
    I've changed the "RequireComponent as you suggested (though Sprite works, as spriterenderer is a component of it, it's clearer that I need the spriterenderer, as that is what is referred in my code)

    Also, I'm starting to see what messed up my mind. The "Order in Layer" is what is returned with the code I used... which was still set at 0. But what I was really after, was the order in which the Sorting Layers are set.
    For instance, I have set the following Sorting Layers:

    Sorting Layers:
    = Layer Default
    = Layer Background
    = Layer Foreground

    This means objects with the Background layer will always be rendered behind the foreground objects if they cross each other. So, the "order" for these layers would be Default = 0, Background = 1, Foreground = 2.

    But... although I'm still curious about how to determine THAT order... I think just using the "Order in Layer" might actually be a better and easier solution then what I was working on.

    So again, thanks
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689