Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Setting renderer.sortingLayerID via code

Discussion in 'Scripting' started by renman3000, Dec 20, 2019.

  1. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    Hi there,
    I want to set the sortingLayerID of an objects renderer, via renderer.sortingLayerID = some_int; My understanding on how to get the int, is to use int layer = LayerMask.NameToLayer("nameOfLayerIWant");

    My issue is that I am getting a return value of layer = -1. This despite having this particular layer, set in the ProjectSettings, see pics.

    Any thoughts?
    Thanks
    https://i.postimg.cc/GhQw16h4/2-C3-C2486-A197-4577-A611-7-EA2-AF155-C5-D.jpg
    https://i.postimg.cc/g229c4nN/5-BAD739-F-29-D4-4040-AFDA-9-EBE81-B69-A15.jpg
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Close! So close... LayerMask is for the 32 bitfield layers that Unity uses for lights, cameras, visibility.

    I'm pretty sure what you want is this:

    https://docs.unity3d.com/ScriptReference/SortingLayer.html

    Unity has Layers, Tags, and SortingLayers... it's super easy to get them crossed up, speaking from my own mixups.