Spinteractive
Spinteractive

Reputation: 261

Uncheck Dropdown Item at Runtime in Unity

This seems trivial but I'm having difficulty.

A user selects an item in a dropdown and this creates a little checkmark next to the item.

I want to deselect it in my code and remove that checkmark.

Any ideas?

Upvotes: 0

Views: 1909

Answers (2)

Guney Ozsan
Guney Ozsan

Reputation: 330

Unfortunately original state of a dropdown is dropdown.value = -1 and there is no way resetting it once modified. It is always greater than 0 once modified, even if you assign -1 to it.

The only workaround is to create a prefab of Dropdown and Destroy & Instantiate it from prefab when resetting. In this case you need to create all the listeners dynamically from the code which renders all editor assignments of a Dropdown useless. You need to use an initializer script.

Upvotes: 0

Programmer
Programmer

Reputation: 125455

You can change which item is selected with Dropdown.value. At-lease, one item must be selected.

I want to deselect the item selected. Restore it back to it's original state.

Get the original item in the Start or Awake function:

public Dropdown dropDown;
private int originalState;

void Awake()
{
    originalState = dropDown.value;
}

When you want to restore it back, restore to that value you saved:

void restoreDropDown()
{
    dropDown.value = originalState;
}

Upvotes: 1

Related Questions