- Home /
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 =
try waiting a frame before checking sortingLayerName? It may take a frame for the value to change.
Nothing ever takes a frame to change—all functions are atomic—unless specifically noted in the docs as an async operation.
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.
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;
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.
Hi, Thanks for the suggestion. "building2" is already a sorting layer defined in my game.
It would seem that it's not...make sure the spelling and capitalization match exactly.
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;
}
}
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 :)
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
Follow this Question
Related Questions
Sprite layer order determined by Y value 4 Answers
Line Renderer and Sprite Z-coordinate always 10? 1 Answer
Special sprites ordering 1 Answer
Per-Sorting Layer Collision 1 Answer
Render particles in front of any sort layer (2D sprites) 3 Answers