- Home /
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
}
}
}
Answer by Davirtuoso · May 06, 2017 at 04:51 PM
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!
Hi and thanks for answering.
Doing it this way is absolutely an option and it's what i'd done initially. I guess my main idea is more about a cosmetic/visual appeal. The additional benefit of making an option non-interactable would be the "---"-string could act as a separator.
Ahhh! That makes more sense now! Well I've not used it myself, but I'm aware of an attribute you can use in dropdowns which would disable a specified index from being selectable in a dropdown list:
DropdownA.items[0].Attributes.Add("disabled","disabled");
I believe that line would get index 0 of DropdownA (which would be 'select option' from the look of things) and disable the ability to interact with it. Hopefully from that you'll be able to adapt that line to do whatever it is you'd like to do for your lists, but the only thing you'd need to change is the DropdownA at the start and the index you want disable if you'd like to use it elsewhere. I hope that works for you :-)
Answer by Jviaches · Jan 22, 2019 at 11:42 PM
for me making non iteractable first options was :
languageDropDwn = GameObject.Find("lang-dropdown").GetComponent<Dropdown>();
languageDropDwn.interactable = false;
Your answer
Follow this Question
Related Questions
Drop down list to inspector 2 Answers
Make use of a button in a drop down menu? 0 Answers
Getting information of which Dropdown menu has been changed 0 Answers