Unity TextMeshPro Dropdown issue

,

Hi Everybody,

I’m building my own UI for my project and I’m using TextMeshPro.
In my UI there is a TMP_dropdown whose options are set through a script which reads an external excel file.
The point is that as soon as the game starts, the first option (added thanks the AddOptions method) is authomatically selected. I want to avoid this and tried to change the Value (the current index selected) setting it to -1 through a simple script, but it doesn’t work.
I make an example for clarity:
my script added 3 different options: red (Value = 0), blue (Value = 1) and yellow (Value =2). If I set the Value to -1 (dropdown.value = -1) the Value goes to 0 and red is selected, while if I set the Value to 10 it goes to 2 and yellow is selected.
The strange thing is that if the game is running I can change the Value through the Inspector and it works.

Can anyone help me with this issue?

Thanks

Fabio

Hi,

Have you already checked the TextMeshPro API and looked up the class you used? Maybe you can find a premade solution for your problem.

Also add a bunch of Debug.Logs to your code to see if your elements get assigned correctly. The TDD (test-driven development) approach could also be interesting for you, so you do not have to test your game manually each time you change something in your UI.

Also please feel free to ask our helpful community of students for advice in our official Discord chat.

Hello Nina,

yes I looked at the Unity scripting API and cannot figure out the problem.
As you can see the value field is public:

namespace TMPro
{
[AddComponentMenu(“UI/Dropdown - TextMeshPro”, 35)]
[RequireComponent(typeof(RectTransform))]
public class TMP_Dropdown : Selectable, IPointerClickHandler, IEventSystemHandler, ISubmitHandler, ICancelHandler
{
protected TMP_Dropdown();

    public RectTransform template { get; set; }
    public TMP_Text captionText { get; set; }
    public bool IsExpanded { get; }
    public int value { get; set; }
    public DropdownEvent onValueChanged { get; set; }
    public Image captionImage { get; set; }
    public TMP_Text itemText { get; set; }
    public Image itemImage { get; set; }
    public List<OptionData> options { get; set; }

and the code doesn’t give me any error while I’m setting the value:

private void Start()
{
    dropdown.value = -1;
    i = dropdown.value;
}

The point is that actually I cannot set a value external to the lenght of the Options as described in my previous message.

Any suggestion?

Thanks

Fabio

I ran into the same problem. My solution may or may not work for you, but what I did was make the very first entry (index 0) a dummy entry labeled “-------------” with no functionality. That way when you first open the dropdown it doesn’t fire off any code. So my dropdown looks something like this:

[MyDropdown]
------------         //<----- index 0; does nothing when selected
Red
Blue
Yellow

This topic was automatically closed after 13 days. New replies are no longer allowed.