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; } }
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()
{
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.