# How to get selected value in a dropdown

The trouble is that dropdowns are not mentioned in there, and I have totally no idea how to get the selected text value from it.

I tried multiple options, but they don't work for me, even if the compiler doesn't return any errors. Maybe the thing is that I'm implementing the code in wrong place or I miss something different.

The solution I'll paste here looks quite logical for me and I have no idea why doesn't it work. The part of code where I tried to 'read' the dropdown value is:

else if (a.GetComponent<Dropdown>()!=null)
{
}

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

public class UIScript07 : MonoBehaviour

{
public GameObject[] questionGroupArr;
public QAClass07[] qaArr;

void Start()
{
qaArr = new QAClass07[questionGroupArr.Length];
}
// Update is called once per frame
void Update()
{

}
{
for (int i = 0; i < qaArr.Length; i++)
{
}
}
{
QAClass07 result = new QAClass07();

GameObject q = questionGroup.transform.Find("Question").gameObject;

result.Question = q.GetComponent<Text>().text;

if (a.GetComponent<ToggleGroup>() != null)
{
for (int i = 0; i < a.transform.childCount; i++)
{
if (a.transform.GetChild(i).GetComponent<Toggle>().isOn)
{
break;
}

}
}

else if (a.GetComponent<Dropdown>()!=null)
{
}
else if (a.GetComponent<ToggleGroup>() == null)
{
string s = "";
int counter = 0;

for (int i = 0; i < a.transform.childCount - 1; i++)
{
if (a.transform.GetChild(i).GetComponent<Toggle>().isOn)
{
if (counter != 0)
{
s = s + ", ";
}
s = s + a.transform.GetChild(i).Find("Label").GetComponent<Text>().text;
counter++;
}
if (i == a.transform.childCount - 1)
{
s = s + "";
}
}

}
return result;
}
}
[System.Serializable]
public class QAClass07
{
public string Question = "";
}



This is really simple, all you need is to access the .value field of the dropdown. This gives you the index that is selected (starting at 0). So for a dropdown with 3 options, you get 0, 1 or 2 back. If you need the exact string, you can go over that with dropdown.options[dropdown.value].text. But you do not need to go the options way since probably load those already from an array.

Small example code with one dropdown and button in the scene, the script is on the button and ClickTest is getting called on button click

public class ClickTest: MonoBehaviour {

[SerializeField] private Dropdown dropdown;

public void SelectedText() {
Debug.Log(dropdown.value);
Debug.Log(dropdown.options[dropdown.value].text);
}
}


To somehow map it to your code, it would look something like that

Dropdown tempDropdown = a.GetComponent<Dropdown>();
if (tempDropdown != null) {

Since we need to access the Dropdown component more often, we temporary store it to not always call a.GetComponent<Dropdown>() again.