Using a first Dropdown menu selection to make a second Dropdown menu visible

Hi, I’m new to object oriented programming, Unity, and this forum. I have a simple menu system comprising two dropdown boxes on a Canvas. The first dropdown box is the only one initially Active or visible in the scene. It has an option that, when selected, I would like to cause the second dropdown box to be visible.

So far, I have attached a simple script with a function to run via the On Value Changed provision in the first dropdown. The script has if statements to handle each of the possible integer selections of the first dropdown and does get called properly.

My problem is:

I cannot figure out how to toggle the visibility of the second dropdown based on the selection or deselection of the associated item in the first dropdown. My approach has been to use gameobject.SetActive() but it creates errors when it is inactive, presumably because it cannot be found when inactive.

Can someone recommend the basic concept, in English, that I should be trying to implement in script to make this happen?

I’m intentionally avoiding asking about any particular script because I’d like to understand the best basic approach to start with.

Thanks in advance!

Hello @lbreton,

@WDLG_RobertIV I’m not sure it should be done in the Update function, he said there is a OnValueChanged event so it should be like this:

private void Start() {
    downTwo.onValueChanged.AddListener(OnDropdownValueChanged);
}

private void OnDropdownValueChanged(int value) {
    Debug.Log("Selected value: " + value);
  
    // Do something based on the selected value
    switch (value) {
        case 0:
            // Action for first option (value == 0) for example hide the dropdown
            this.downTwo.gameObject.SetActive(false);
            Debug.Log("First option selected");
            break;
        case 1:
            // Action for second option and so on
            Debug.Log("Second option selected");
            break;
        default:
            Debug.Log("Other option selected");
            break;
    }
}

the gameObject function takes a boolean as parameter to define the state of the gameObject you are accessing

in basic form?

[SerializeField] public TMP_Dropdown downone;
[SerializeField] public TMP_Dropdown downTwo;

void Update()
{
    if(downone.value == 1)
    {
        //You chose value B
        downTwo.gameObject.SetActive(true);
        downone.gameObject.SetActive(false);
    }   
}

I would have 2 scripts one on each dropdown and I would use the first one to trigger a function to hide or show the second dropdown via the script on it