dynamically fill dropdown unity

Ok here’s the problem. I try to make a system who fill a dropdown with information than the player select previously. We got the information but I can’t find a way to fill the dropdown I tried many things but nothing work. And I have an error :
NullReferenceException: Object reference not set to an instance of an object
DropdownSort.Start () (at Assets/Script/SpellCreation/CreationSpell/DropDownOffensif/DropdownSort.cs:28)

Here’s the code of the dropdown.

public class DropdownSort : MonoBehaviour
{

public Element2 recupElement;
public List listElement;
public string elementChoisie;

// Start is called before the first frame update
void Start()
{
Dropdown dropdownElement = transform.GetComponent();

dropdownElement.ClearOptions(); // line 28 the error

List enumList = new List();
// int currentEnumIndex = 0;
for (int i = 0; i < listElement.Count; i++)
{
string option = listElement*.ToString();*
enumList.Add(option);

}
dropdownElement.AddOptions(enumList);
//dropdownElement.value = listElement*;*
dropdownElement.RefreshShownValue();
dropdownElement.enabled = false;
dropdownElement.enabled = true;
dropdownElement.Show();
}
// Update is called once per frame
void Update()
{
}
public void DropdownItemselected(Dropdown dropdownElement)
{
int index = dropdownElement.value;
elementChoisie = dropdownElement.options[index].text;
}
}

Make sure that your DropdownSort object has a Dropdown component. If it does not then you can do this:

public Dropdown dropdownElement;

then drag the dropdown onto your dropdown field in the inspector.

You are missing a list index here:

    string option = listElement[i].ToString();

However, you can replace your loop with AddRange();

dropdownElement.enabled = false;
dropdownElement.enabled = true;

This makes no sense.

OK since I post that I make modification but nothing is show in the dropdown. I got the two option selected but no text in. And no more error find actually. And I do the affectation in the inspector with the public dropdown.
The code :

public Element2 recupElement;
public List listElement;
public string elementChoisie;
public TMP_Dropdown dropdownElement;

// Start is called before the first frame update
void Start()
{

listElement = recupElement.elements;

dropdownElement.ClearOptions();
Debug.Log(dropdownElement);

List enumList = new List();

for (int i = 0; i < listElement.Count; i++)
{
string option = listElement*.ToString();*
enumList.Add(option);

}
dropdownElement.AddOptions(enumList);
dropdownElement.RefreshShownValue();

//dropdownElement.enabled = false;
//dropdownElement.enabled = true;

}

Here’s how to post code on the Unity forums: Using code tags properly

string option = listElement.ToString();

You didn’t fix it.

Oh ok I didn’t see it my bad. Now I have something in my dropdown : but it’s not what I excepted

9004219--1240714--upload_2023-5-10_13-23-14.png

But I guess it’s the good way
I forgot to say what we see on the screen it’s here at the start

What you see is listElement.ToString(), to get the item you need

listElement[i]

instead,

listElement[i].ToString()

is not necessary.

I cut the ToString(), but I can’t turn a list of string into a string , that’s the message in visual

Ok, try this instead:

foreach (var option in listElement) {
    enumList.Add(option);
}

Ok I try this, now there is no text or something else. And no compil error

Edit : in the editor listElement had the “elements” than we selected early. It’s just, we can’t find a way to update the dropdown with those informations

Your first issue was that you named your list “listElement”, it’s better practice to make it plural so that you don’t get confused, so you could name it listElements instead. To access each individual element of the list you needed to use the brackets [ ] as I have shown to you.

Anyway, you don’t even need this, you can just do:

dropdownElement.AddOptions(listElement);

If it does not work, select the dropdown in the inspector, you will see its options and whether it is active or not.

OK problem solve. We put all the code in the start and we had a if to run it one time and that’s it it’s working.
Thank you for your help.

1 Like