1
\$\begingroup\$

I made a simple survey from a guide on YouTube.

Screenshot of survey UI in Unity

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)
{
    result.Answer = a.transform.GetChild(0).Find("Label").GetComponent<Text>().text;
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
using System;

public class UIScript07 : MonoBehaviour

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

    void Start()
    {
        qaArr = new QAClass07[questionGroupArr.Length];
    }
    // Update is called once per frame
    void Update()
    {
        
    }
    public void SubmitAnswer()
    {
        for (int i = 0; i < qaArr.Length; i++)
        {
            qaArr[i] = ReadQuestionAndAnswer(questionGroupArr[i]);
        }
    }
    QAClass07 ReadQuestionAndAnswer(GameObject questionGroup)
    {
        QAClass07 result = new QAClass07();

        GameObject q = questionGroup.transform.Find("Question").gameObject;
        GameObject a = questionGroup.transform.Find("Answer").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)
                {
                    result.Answer = a.transform.GetChild(i).Find("Label").GetComponent<Text>().text;
                    break;
                }

            }
        }
        
        
    else if (a.GetComponent<Dropdown>()!=null)
        {
            **result.Answer = a.transform.GetChild(0).Find("Label").GetComponent<Text>().text;**
        }
        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 + "";
                }
            }

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

\$\endgroup\$

2 Answers 2

2
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\begingroup\$ Thank you for answering that quick. It looks pretty easy, but I'm a hopeless newbie and still have no idea how to implement it in my code :( I need the selected text to appear as my "answer" in array created in preceding code \$\endgroup\$ Commented May 25, 2022 at 18:30
  • \$\begingroup\$ If you have already trouble with the basics and this is part of your assignment, you might have to spent a bit more time following a tutorial or read up about it. Once you have to explain why you did a certain part or apply it to a different problem you encounter, it is likely to be stuck again \$\endgroup\$ Commented May 25, 2022 at 19:11
  • \$\begingroup\$ You're absolutely right sir, I need to work on it, but there is a hope for me, since my degree is not absolutely related with coding, I'm on totally different field of study, this is just a part of my work. I'll definitely try to learn it. Thank you for your help, and time. I'm really grateful. It works, but if someone wants to do something equal from guide mentioned above with dropdowns - you have to rename them to "Answer" instead of putting dropdown in empty object called "Answer". Then the code with Zibelas' help works perfectly. \$\endgroup\$ Commented May 26, 2022 at 6:50
0
\$\begingroup\$

For new Dropdown component in Unit, add the Dropdown component to your inspector and in OnValueChanged section, reference the method below (Don't forget to send itself as a parameter):

public void OnDropdownChanged(Dropdown dropdown) {
    var newSelectedIndex = dropdown.value;
    var newSelectedValue = dropdown.options[newSelectedIndex].text;
    var newSelectedSprite = dropdown.options[newSelectedIndex].image;
    //...
}

If you use TextMeshPro Dropdown change the parameter class with TMP_Dropdown:

public void OnDropdownChanged(TMP_Dropdown dropdown)
\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.