Search Unity

Bug dynamically fill dropdown unity

Discussion in 'Scripting' started by Hiro_Takahashi15, May 9, 2023.

  1. Hiro_Takahashi15

    Hiro_Takahashi15

    Joined:
    Apr 7, 2023
    Posts:
    11
    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<string> listElement;
    public string elementChoisie;


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


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

    List<string> enumList = new List<string>();
    // 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;
    }


    }
     
  2. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    699
    Make sure that your DropdownSort object has a Dropdown component. If it does not then you can do this:

    Code (csharp):
    1.  
    2. public Dropdown dropdownElement;
    3.  
    then drag the dropdown onto your dropdown field in the inspector.


    You are missing a list index here:
    Code (csharp):
    1.  
    2.     string option = listElement[i].ToString();
    3.  
    However, you can replace your loop with AddRange();


    https://learn.microsoft.com/fr-fr/d....addrange?source=recommendations&view=net-7.0

    Code (csharp):
    1.  
    2. dropdownElement.enabled = false;
    3. dropdownElement.enabled = true;
    4.  
    This makes no sense.
     
  3. Hiro_Takahashi15

    Hiro_Takahashi15

    Joined:
    Apr 7, 2023
    Posts:
    11
    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<string> 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<string> enumList = new List<string>();

    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;




    }
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
  5. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    699
    Code (csharp):
    1.  
    2. string option = listElement.ToString();
    3.  

    You didn't fix it.
     
  6. Hiro_Takahashi15

    Hiro_Takahashi15

    Joined:
    Apr 7, 2023
    Posts:
    11
    Oh ok I didn't see it my bad. Now I have something in my dropdown : but it's not what I excepted

    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
     
    Last edited: May 10, 2023
  7. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    699
    What you see is listElement.ToString(), to get the item you need

    Code (csharp):
    1.  
    2. listElement[i]
    3.  
    instead,
    Code (csharp):
    1.  
    2. listElement[i].ToString()
    3.  
    is not necessary.
     
  8. Hiro_Takahashi15

    Hiro_Takahashi15

    Joined:
    Apr 7, 2023
    Posts:
    11
    I cut the ToString(), but I can't turn a list of string into a string , that's the message in visual
     
  9. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    699
    Ok, try this instead:

    Code (csharp):
    1.  
    2. foreach (var option in listElement) {
    3.     enumList.Add(option);
    4. }
    5.  
     
  10. Hiro_Takahashi15

    Hiro_Takahashi15

    Joined:
    Apr 7, 2023
    Posts:
    11
    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
     
  11. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    699
    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:

    Code (csharp):
    1.  
    2. dropdownElement.AddOptions(listElement);
    3.  


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

    Hiro_Takahashi15

    Joined:
    Apr 7, 2023
    Posts:
    11
    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.
     
    dlorre likes this.