Search Unity

Dropdown list appears behind everything...

Discussion in 'UGUI & TextMesh Pro' started by Alimaggs, Sep 23, 2015.

  1. Alimaggs

    Alimaggs

    Joined:
    Mar 26, 2015
    Posts:
    9
    Hi,

    Was wondering if anybody can help me. I've got a Canvas which has its Render Mode set to World Space.

    Within this canvas, I have a Dropdown (from the new UI tools).

    The problem is, the content of this dropdown appears behind other elements in my scene. In the screenshot, you'll see it appears behind the canvas' background...

    ui-stuff.PNG

    If I add other elements to the scene, such as a background - NOT in the canvas, but on a background sorting layer - the dropdown list appears behind that too...

    ui-stuff-2.PNG


    Not sure what else to try... I've tried setting sorting layers but I can't find a way to set the sorting layer of the dropdown list...

    If I'm in Play mode, I can see the "Dropdown List" appear in the Hierarchy, and if I modify the sorting layer to my UI layer, the list shows up fine. But of course, I can't save this during Play mode, and once I exit Playmode, I lost the ability to edit the Dropdown List.

    SortingLayerShown.JPG

    Any ideas? Thanks!! :)

    Ali
     
  2. sandboxed

    sandboxed

    Unity Technologies

    Joined:
    Apr 6, 2015
    Posts:
    95
    UI image sorting is based on position in hierarchy. Try moving your dropdown list up higher in the hierarchy.
     
    tigerleapgorge likes this.
  3. Trollg

    Trollg

    Joined:
    May 22, 2014
    Posts:
    14
    I have the same problem :confused:. Any solutions to this?

    The dropdown list appears behind the other elements because its canvas has the sorting layer set to default while my other elements are in another sorting layer. Can I set the sorting layer of the dropdown list somewhere? It should inherit the value from the parent canvas.

    Thanks
     
    Gibbonfiend likes this.
  4. Trollg

    Trollg

    Joined:
    May 22, 2014
    Posts:
    14
    I made this lame workaround but I think it should be fixed to inherit the canvas value from the parent

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using UnityEngine.EventSystems;
    4. using System.Collections;
    5.  
    6. public class DropdownCanvasLayer : MonoBehaviour {
    7.  
    8.     public string SortLayerName;
    9.     public EventSystem eventSystem;
    10.    
    11.     public void Update(){
    12.  
    13.         if (Input.GetMouseButtonDown (0)) {
    14.  
    15.             if (eventSystem.IsPointerOverGameObject() && eventSystem.currentSelectedGameObject.name == "Dropdown"){
    16.  
    17.                 StartCoroutine(findList());
    18.             }
    19.         }
    20.  
    21.     }
    22.  
    23.     IEnumerator findList(){
    24.  
    25.         yield return new WaitForSeconds (0.2f);
    26.         GameObject droplist = GameObject.Find ("Dropdown List");
    27.         GameObject blocker = GameObject.Find ("Blocker");
    28.  
    29.         if (droplist != null) {
    30.            
    31.             droplist.GetComponent<Canvas> ().sortingLayerName = SortLayerName;
    32.             blocker.GetComponent<Canvas> ().sortingLayerName = SortLayerName;
    33.         }
    34.     }
    35.  
    36. }
    37.  
     
  5. UniKid

    UniKid

    Joined:
    Nov 17, 2015
    Posts:
    1
    I have the same problem but I made a simple workaround that only checks and updates the sorting layer whenever the Dropdown List template is spawned.

    Note:
    Put this script on the Template game object inside the Dropdown. You need to change the value of _SortingLayerName to the SortingLayer of the parent canvas where your Dropdown resides.

    public class DropdownWorkaround : MonoBehaviour
    {
    public string _SortingLayerName = "Default";

    void Awake()
    {
    Canvas canvas = GetComponent<Canvas>();
    if (canvas != null)
    canvas.sortingLayerName = _SortingLayerName;
    }
    }
     
  6. bdandre_mbiance

    bdandre_mbiance

    Joined:
    Nov 6, 2015
    Posts:
    1
    Hi everyone,

    I just find out that if you add a Canvas to your Template game object of your Dropdowns with the wanted sorting layer it works like a charm!
     
  7. juliocamargo86

    juliocamargo86

    Joined:
    Nov 24, 2016
    Posts:
    1
    I solved this problem just setting the 'Default' Sorting Layer in front of all the others (i.e., at the bottom of the Sorting Layer list). Since I avoid to use the 'Default' Sorting Layer in my game objects, this change not cause any problem.
     
    ovirta likes this.
  8. leyend1000

    leyend1000

    Joined:
    Jul 14, 2016
    Posts:
    2
    Perfect thanks :)
     
    CrandellWS likes this.
  9. Jinxology

    Jinxology

    Joined:
    Jul 13, 2013
    Posts:
    95
    Adding to this, I actually added a Canvas to the Template, checked Override Sorting, and set the Sorting Layer to the layer that you want the dropdown list to be on.
     
    CrandellWS, TDA777, myama and 2 others like this.
  10. NYUnity

    NYUnity

    Joined:
    Aug 11, 2012
    Posts:
    8
    This fixed the problem for me, after an hour of pulling my hair out. Thank you! (Unity 5.5.4 on macOS. Yes, afraid to update until I have a few weeks to make sure everything works on 2017.X.)
     
  11. pixelR

    pixelR

    Joined:
    Sep 16, 2013
    Posts:
    58
    After lots of research you've just saved my sanity, thanks! :D
     
  12. MoFaShi

    MoFaShi

    Joined:
    Oct 25, 2015
    Posts:
    43
    Perfect, you saved my life.
     
  13. ookk47oo

    ookk47oo

    Joined:
    Mar 17, 2017
    Posts:
    80
    Code (CSharp):
    1.            
    2. Canvas popupCanvas = GetOrAddComponent<Canvas>(templateGo);
    3.  popupCanvas.overrideSorting = true;
    4.  popupCanvas.sortingOrder = 30000;
    5.  
    The DropDown's source code added a canvas whose sortingorder is 30000.

    One solution is like Jinxology's said.However, I prefer to change the source code and customize my own dropdown.
     
  14. gaopeter

    gaopeter

    Joined:
    Apr 23, 2020
    Posts:
    2
    Perfect, thanks
     
  15. maheshwarirohit14

    maheshwarirohit14

    Joined:
    Dec 11, 2018
    Posts:
    3
    There is a Template gameobject in dropdown object, add a raycaster on that and set the sort order.
     
  16. FGFFFF00

    FGFFFF00

    Joined:
    Oct 9, 2020
    Posts:
    2
    at version 2019.4 add canvas on Template not work ,
    i take a toggle in DropDown and listening the value to set DropDown Show or Hide ,
    when show get canvas component and set sorting Layer
     
    Gibbonfiend likes this.
  17. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    I'm going to move this to the UI forums because the 2D forum is not for UI related questions.
     
  18. AlexisTerraf

    AlexisTerraf

    Joined:
    Oct 31, 2016
    Posts:
    6
    Just would like to add that in Unity 2021.2, if you manually add the Canvas to the the "Template" GameObject and set the layer you want it to use, Unity will override all values on play. So it seems that the solution proposed by bdandre_mbiance above which apparently used to work as far as November 2020, does not work anymore.
     
    travlake, awallick-sd and BV0176 like this.
  19. artbotva

    artbotva

    Joined:
    Nov 8, 2014
    Posts:
    23
    Solution for new version:

    In TMP_Dropdown.cs change 666 line to sorting number you whant, then in 779 line change to this:
    m_Template.GetComponent<Canvas>().sortingLayerID = SortingLayer.NameToID("Your TargetLayer");
     
  20. won-gyu

    won-gyu

    Joined:
    Mar 23, 2018
    Posts:
    35
    Code (CSharp):
    1.     public class DropdownList : MonoBehaviour
    2.     {
    3.         [SerializeField]
    4.         private Canvas canvas;
    5.  
    6.         private void OnEnable()
    7.         {
    8.             canvas.sortingLayerName = "Your own layer";
    9.         }
    10.     }
    attach it onto "Template" and it should work