Search Unity

Load Dropdown with data from list

Discussion in 'UGUI & TextMesh Pro' started by zeb33, Jun 23, 2016.

  1. zeb33

    zeb33

    Joined:
    Nov 17, 2014
    Posts:
    95
    Hi
    Am going in circles trying to see what am doing wrong. AM trying to populate 4 dropdowns (TextMeshPro adopted) with list data. Could have done it quicker by hand using the inspector , but once committed...

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine.UI;
    6. using TMPro;
    7.  
    8. public class DropdownController : MonoBehaviour
    9. {
    10.     public TMP_Dropdown dropdown0;
    11.     public TMP_Dropdown dropdown1;
    12.     public TMP_Dropdown dropdown2;
    13.     public TMP_Dropdown dropdown3;
    14.  
    15.  
    16.  
    17.     //private TMP_Dropdown.DropdownItem SelectedItem { get; set; }
    18.    
    19.     //private List<Dropdown.OptionDataList> Items;
    20.    
    21.    
    22.     public List<string> list = new List<string>() {"test(po)", "another (po)", "eye (po)", "brain (iv/sc) };
    23.  
    24.    //public TMP_Dropdown dropdown0.AddOptions(List<string> list);
    25.  
    26.    void dropdown0.ClearOptions();
    27.  
    28.    void dropdown0.AddOptions(List<string> list);
    29.  
    30.    //GetComponent<TMP_Dropdown>().dropdown0.options.Add(list);
    31.  
    32.  
    33.    //dropdown0.options.Add(new Dropdown.OptionData(option));
    34.  
    35.    //void dropdown0.options.Add.OptionData(List<string> list)
    36.  
    37. }
    38.  

    latest out of many error sis Assets/DropdownController.cs(35,14): error CS0118: `DropdownController.dropdown0' is a `field' but a `type' was expected
    Frustrating as I had it working hours ago.
     
    Last edited: Jun 23, 2016
  2. zeb33

    zeb33

    Joined:
    Nov 17, 2014
    Posts:
    95
    got it.. think was trying to access text component rather than tmp_text!

    Code (CSharp):
    1. public List<string> list = new List<string>() {"test0 (to)", "test1 (t1)" };
    2. public TMP_Dropdown    TMPDropdown;
    3. public TMP_Text        text;
    4.  
    5. void Start () {
    6.    
    7.      TMPDropdown.options.Clear ();
    8.      foreach (string t in list)
    9.      {
    10.          TMPDropdown.options.Add (new TMP_Dropdown.OptionData() {text=t});
    11.      }
    12. }
     
    Baykus, Ryuuguu, koen-rm and 8 others like this.
  3. Uepilon

    Uepilon

    Joined:
    May 1, 2020
    Posts:
    1
    Yes, this works. Thank you! :)
     
  4. JBR-games

    JBR-games

    Joined:
    Sep 26, 2012
    Posts:
    708
    Code (CSharp):
    1.  
    2.  // TMPro  dropdown setup
    3.         dropDown.ClearOptions();
    4.        int enumSize = System.Enum.GetValues(typeof(JBR_Enums.ResourceType)).Length;
    5.  
    6. //creates a new list
    7.         List<TMP_Dropdown.OptionData> data = new List<TMP_Dropdown.OptionData>();
    8.      
    9.         for (int i = 0; i < enumSize; i++)
    10.         {
    11. //create a new item for list
    12.             TMP_Dropdown.OptionData newData = new TMP_Dropdown.OptionData();
    13.             newData.text = ((JBR_Enums.ResourceType)i).ToString();
    14.             data.Add(newData);      
    15.         }
    16. //populate TMPro dropdown with  List
    17.         dropDown.AddOptions(data);
    In this case im using an enum to populate my TMpro dropdown
     
    danosono likes this.