Make Dropdown option non-interactable

Hi everybody!

I ran into a problem for what i didn’t found a solution or maybe i was researching in the wrong direction entirely.

I have 2 Dropdowns, each populated with a String-List. I want to look if the selected option of DropdownA also exists in DopdownB and make it non-interactable.
I just can’t figure out how to access variables of a specific/single option.

At this point, i feel like pretty much anything would help. Thanks in advance.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class dropdowntest : MonoBehaviour
{
    List<string> listA = new List<string>() { "select option", "---", "option1", "option2", "---", "option3", "option4" };
    List<string> listB = new List<string>() { "select option", "---", "option1", "option2", "---", "option3", "option4", "option5", "option6" };
    public Dropdown dropdownA, dropdownB;


    void Start()
    {
        PopulateLists();
    }

    void PopulateLists()
    {
        dropdownA.AddOptions(listA);
        dropdownB.AddOptions(listB);
    }

    // get text of selected dropdownA option and make DropdownB option with the same string non-interactable
    public void non_interactable()
    {
        {
            // make DropdownB option interactable = false
        }
    }
}

I actually have done something similar in my most recent project, though not quite the same. For me, I was selecting something from my first dropdown, and then removing it from the list in my second dropdown completely so it couldn’t be selected. In my case, both lists were replicas of eachother so, index 3 of list one was the same as index 3 of list two. I’m not sure if this would actually be an option for you or not, but essentially to do this you could create a method that runs when dropdown A is changed (something you can set up in unity):

public void UpdateListB()
{
    int toRemove = DropdownA.GetComponent<Dropdown> ().value;
	DropdownB.GetComponent<Dropdown> ().ClearOptions ();
	List DropBList = new List<string> (listB);
	DropBList.RemoveAt(toRemove);
    DropdownB.GetComponent<Dropdown>().AddOptions(DropBList);
}

That would essentially find the index of the item in Dropdown A, clear Dropdown B, create a temporary list containing all of the items from listB, remove the item at the same index in Dropdown B then finally populate Dropdown B with all of the remaining items of your temporary list.

It worked like a charm for what I needed, I hope it helps you too. Good luck!

for me making non iteractable first options was :

    languageDropDwn = GameObject.Find("lang-dropdown").GetComponent<Dropdown>();
    languageDropDwn.interactable = false;

It’s too bad that the Dropdown.OptionData (also for TMP_Dropdown) doesn’t come with an interactable property. Here is my small workaround:

  • From the scene I figured out that the option is actually a Toggle component.

  • created a small method to get the Dropdown List which is added in the scene after user clicked on the dropdown

  • get all Toggles with GetComponentsInChildren

  • loop through toggles and set interactable of desired option

     // Update is called once per frame
     void Update()
     {
         var dropdownList = dropdown.gameObject.transform.Find("Dropdown List");
         if (dropdownList != null)
         {
             SetInterceptableOfDropdownOption("Weiblich (coming soon)");
         }
     }
    
     /**
      * Sets interceptable of an option according to option text.
      */
     void SetInterceptableOfDropdownOption(string optionTextToDeactivate)
     {
         var toggles = dropdown.gameObject.transform.Find("Dropdown List").GetComponentsInChildren<Toggle>();
         foreach (var toggle in toggles)
         {
             var toggleText = toggle.transform.Find("Item Label").GetComponent<TextMeshProUGUI>().text;
             if (toggleText == optionTextToDeactivate)
             {
                 toggle.interactable = false;
             }
         }
     }