[SOLVED] Dropdown opens only once

Hello there, before posting this thread I’ve made a lot of researches these last days, and I’ve found some topics dealing the same issue but I found nothing really relevant for me. I’ve tried a lot of solutions but none of them worked on the long-term - or didn’t work at all.

I have a button called “Load Button”. When I click on it, it displays a panel which has a Dropdown as child. When I click on this Dropdown, it displays a list of maps that can be loaded by the rest of my script. When one of these maps is loaded, the panel is automatically hidden, and so is the dropdown. Clicking once again on the Load Button re-displays it.

However, when I try to load another map by clicking on the Dropdown, nothing appears. The Dropdown is stuck and doesn’t display any of its options (although they are all added to the list, I triple checked that).

When my dropdown is active :

And once I’ve load a map (in this example, “tamere”) :

No matter how many times I click on it, my dropdown remains like that. it IS interactable, it has all its options… I don’t know why. It seems that is a bug found by the Unity Issue Tracker that cannot be fixed, but I hope it is just a mistake from me (it will piss me off if I can’t fix that).

So, here is my code now :

public GameObject panelLoad;
    [SerializeField] private Dropdown dropdownLoad;


    public bool showGUI;

 
    private string filePath;

    void Awake()
    {
        panelLoad = GameObject.Find("Panel Load");
        dropdownLoad = GameObject.Find("Dropdown Load").GetComponent<Dropdown>();

    }






    public void BUTTON_LOAD()
    {
        ADD_OPTIONS_TO_DROPDOWN();
        DISPLAY_PANEL();

    }


    public void ADD_OPTIONS_TO_DROPDOWN()
    {

      
        UnityEngine.Object[] allTextFiles = Resources.LoadAll("Maps", typeof(TextAsset));
        //print(fileInfo.Length);
        List<string> dropOptions = new List<string>(allTextFiles.Length);


        for (int i = 0; i < allTextFiles.Length; i++)
        {
          
                dropOptions.Add(allTextFiles[i].name);
                //print(dropOptions[i]);

                if (dropdownLoad.options.Count > 0)
                {
                    dropdownLoad.ClearOptions();
                }

            dropdownLoad.AddOptions(dropOptions);
            dropdownLoad.RefreshShownValue();
        }


    }


    public void DISPLAY_PANEL()
    {

        showGUI = !showGUI;
        Proceed_To_showGUI_Verification();
       
    }


    public void Proceed_To_showGUI_Verification()
    {
            panelLoad.SetActive(showGUI);
    }




    public void START_LOAD()
    {

        filePath = string.Concat("Assets/Resources/Maps/" + dropdownLoad.captionText.text + ".txt");

        if (!File.Exists(filePath))
            error.DISPLAY_ERROR_MESSAGE(14, "Error", "The file you are trying to load does not exist.");
        else
        {
            //print(filePath);
            LOAD_MAP_FROM_FILEPATH();
        }
    }

Don’t bother about LOAD_MAP_FROM_FILEPATH(), it has nothing to do with my problem. I’m sure the bug comes from panelLoad.SetActive(showGUI), since panelLoad is the parent of dropDownLoad, but I can’t figure out why, since the dropDown gameObject is disabled as soon as its parent itself is disabled. But the “why?” is a mystery for me.

Thanks for your answer.

You are doing things inside the for loop that should be outside, lines 8 - 16 should be outside the loop. If the line numbers change, it’s marked with a comment.

        for (int i = 0; i < allTextFiles.Length; i++)
       {
  
                dropOptions.Add(allTextFiles[i].name);
                //print(dropOptions[i]);
        //from here down should be outside the loop.
                if (dropdownLoad.options.Count > 0)
                {
                    dropdownLoad.ClearOptions();
               }


            dropdownLoad.AddOptions(dropOptions);
           dropdownLoad.RefreshShownValue();
        }

Thanks for pointing this out, but it doesn’t solve my problem. I still can’t open my Dropdown list :confused:

Hello tranos, I see what you’re talking about, but it’s impossible. When I click on the Load Button, it calls the Display_Panel() method which automatically switches the state of showGUI. When the panel is visible, showGUI is forced to be true, and so the Dropdown is visible and active as well. When I click once again on the button, or when I load a new map, showGUI becomes false, and so my panel and its dropdown become disabled as well.

Since the disabling method is the last thing the script does before becoming inactive, I’m pretty sure the problem doesn’t come from showGUI itself.

And, another thing I forgot to mention : If I repetedly activate/deactivate the panel, I can still display the dropdown list at any time. However, as soon as I’ve loaded another map, the dropdown is stuck.

Another MAP, not another SCENE : The map I’m loading is stored inside a .txt file, so the scene remains the same before and after the loading process. So it’s not a problem concerning a missing reference or things like that.

In for loop when it adds an option, in the next iteration the if statement clears the the dropdownLoad options.

Try something like this:

public void ADD_OPTIONS_TO_DROPDOWN()
    {
  
        UnityEngine.Object[] allTextFiles = Resources.LoadAll("Maps", typeof(TextAsset));
        //print(fileInfo.Length);
        List<string> dropOptions = new List<string>(allTextFiles.Length);

         if (dropdownLoad.options.Count > 0)
         {
               dropdownLoad.ClearOptions();
         }

        for (int i = 0; i < allTextFiles.Length; i++)
        {
      
                dropOptions.Add(allTextFiles[i].name);
                //print(dropOptions[i]);                                 
        }
        dropdownLoad.AddOptions(dropOptions);
        dropdownLoad.RefreshShownValue();
    }

Already fixed, and no, the issue does not come from there.

If you add a default Dropdown gameobject does it work properly? Do you have the latest version of Unity?

Yes, and yes. The dropdown I currently have is already a default dropdown gameObject. I tried replacing it by a new one but it stays the same.

I know that is a default. I ask if it is working properly if you don’t mess with it via code and simply try to choose the default options. Does it do the same?

Well, I’ve removed the function called by OnValueChanged, and I tried what you suggested. It works, yes, I clicked on my options and I can hide/display the list whenever I want. But as soon as I add my function DISPLAY_PANEL(), which is necessary for my script, the list is stuck and won’t display.

You need to do another debug on dropOptions when it’s outside the loop. Using a for loop to print out all values. Also, posting your fixed code would help. The most likely place is when clear options is called. It’s basically doing the same thing the second time, so most of your code is obviously good.

If there is actually information in there, I’m wondering if you can do a clear options on the same frame.

Done. Just to be sure, I’ve placed a FOR loop before and another one after the ClearOptions(). When it executes my code for the first, time, it displays nothing because my dropdown is empty. So, everything is normal, and when I press the Load Button a second time, only the first FOR loop is called, which means the ClearOptions() method does its job.

My fixed code has nothing special, I’ve just applied what you suggested in your first reply. But if you want to see it :

 public void ADD_OPTIONS_TO_DROPDOWN()
    {
       
        UnityEngine.Object[] allTextFiles = Resources.LoadAll("Maps", typeof(TextAsset));
        //print(allTextFiles.Length);
        List<string> dropOptions = new List<string>(allTextFiles.Length);
       

        for (int i = 0; i < allTextFiles.Length; i++)
        {
                dropOptions.Add(allTextFiles[i].name);
                //print(dropOptions[i]);
        }




        if (dropdownLoad.options.Count > 0)
        {

            for (int i = 0; i < dropdownLoad.options.Count; i++)
            {
                print(dropOptions[i]);
            }


            dropdownLoad.ClearOptions();


            for (int i = 0; i < dropdownLoad.options.Count; i++)
            {
                print(dropOptions[i]);
            }
        }


        dropdownLoad.AddOptions(dropOptions);
        dropdownLoad.RefreshShownValue();


    }

These are all the changes I made, nothing more.

Did you get values on the second pass then, in the first loop?
You should have used dropOptions.Count for the upper limit, not dropdownLoad.Count, although they may be referencing the same array.

If dropOptions didn’t print in the second loop, that means they were erased. That’s not what you want erased. It’s the array that gets added in AddOptions. That means you would have to call it, clear, first, before you do anything.

DropOptions is automatically erased when I recreate it with new List. What I want to erase are the options stored in dropdownLoad.options. Instead of writing :

 for (int i = 0; i < dropdownLoad.options.Count; i++)
            {
                print(dropOptions[i]);
            }

I should have written :

 for (int i = 0; i < dropdownLoad.options.Count; i++)
            {
                print(dropdownLoad.options[i].text);
            }

So that we can clearly see that the dropdown list is indeed cleared before resuming the execution. DropOptions did not print in the second loop because, just as you pointed out, I used dropdownLoad.Count for the upper limit, which means that no matter how many strings DropOptions had, they would never be printed since dropdownLoad was cleared before going through the loop. I just fixed this mistake, sorry for that :confused:

No. You just want to know if there is information in dropOptions. That’s what gets added to the options after it is cleared. The other does not matter for this problem because we know it basically works. What we want to know if there is information in dropOptions before it gets added. To do that, you have to use the count in dropOptions as the upper limit.

It’s blank, so we want to know if no information was added, or that information was added and somehow got deleted.

Just tested it. I’ve modified and moved my for loops, and yes, DropOptions is not empty when it gets added to the dropdown llist.

Try not using clearOptions at all, and see if the list doubles.

Tried it ; yes, it doubles.

Ok, I’ve solved my problem by finding a solution to another problem I had : Since disabling my panel was what blocked the dropdown right after loading the map, I’ve decided to let the player close it manually either by clicking on the button again, or outside the panel (I have another script that detects a click outside the panel and automatically disables it). Doing the thing this way let me display the dropdown list and collapse it without getting it stuck. Thanks once again for your help, guys :slight_smile:

1 Like

Glad you got it worked out.