How can i add a new item in dropdown options ?
Hi
I use Unity 5.2 version and i want to add a new item in dropdown options when i click into dropdown button using c#
i can clear option when i select an item from dropdownlist (see script bellow )but i want clear when i clicked into dropdown buton(onclick() don't exsit in dropdown script)
public Dropdown dd; dd.GetComponent().options.Clear() ;// clear option when i select an item from dropdownlist
thank you in advance
options.add();
you can use to add a new item in dropdown options
i dont know how to use that, looking for it my self but thats the function
Answer by Trigun666tn · Nov 22, 2015 at 02:28 AM
using UnityEngine; using System.Collections; using UnityEngine.UI; using System.Collections.Generic; using System.IO.Ports;
public class dd_Menu : MonoBehaviour {
public Dropdown Maindropdown;
public Text text;
public string[] ports;
public static SerialPort sp = new SerialPort ("COM1",9600);// Refrence to serialPort
void Start () {
// Fill ports array with COM's Name available
ports = SerialPort.GetPortNames ();
//clear/remove all option item
Maindropdown.options.Clear ();
//fill the dropdown menu OptionData with all COM's Name in ports[]
foreach (string c in ports)
{
Maindropdown.options.Add (new Dropdown.OptionData() {text=c});
}
//this swith from 1 to 0 is only to refresh the visual DdMenu
Maindropdown.value = 1;
Maindropdown.value = 0;
}
void Update () {
//COM port Name that is currently selected on the dropDown Menu
text.text = ports [Maindropdown.value];
}
} Hey anis i use this script for my ports i think you will find every think you need in it . if you want to make the .clear() work with OnClick event you have to create another button/toggle and affect this function to it .
This was helpful as a dropdown example, but for future reference ins$$anonymous$$d of setting value twice you can do
$$anonymous$$aindropdown.RefreshShownValue();
Your answer
![](https://koobas.hobune.stream/wayback/20220612035835im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Accessing my 'Options' panel from another script. 1 Answer
How to Setactive(true) the other class ? 0 Answers
OSX: The application is missing required entitlement com.apple.developer.icloud-services 1 Answer
Unity Ads not showing in editor (dummy ad placement is not showing too) as well as in the build. 2 Answers
Collision problems 0 Answers