Skip to main content
added 380 characters in body
Source Link
Zibelas
  • 5.1k
  • 2
  • 15
  • 26

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) {
    result.Answer = tempDropdown.options[tempDropdown.value].text;
}

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

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);
    }
}

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) {
    result.Answer = tempDropdown.options[tempDropdown.value].text;
}

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

Source Link
Zibelas
  • 5.1k
  • 2
  • 15
  • 26

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);
    }
}