Search Unity

Resolved Make dropdown do something even when value is not changed

Discussion in 'UGUI & TextMesh Pro' started by Hexolenn, Apr 10, 2023.

  1. Hexolenn

    Hexolenn

    Joined:
    Dec 27, 2021
    Posts:
    44
    Right now I am trying to make dropdown menu work like a button. I mostly works fine but when I try to select an already selected item it doesn't do anything because the value doesn't change and I use OnValueChanged for my functions. This also prevents me from pressing the first item from the list because it starts with the index 0.
    How can I make it so that it manages to trigger OnValueChanged on clicking already selected items.
    I tried doing this by making the value -1 after every change but that gives me a stackoverflow error (It works for deselecting the first item though. It just gives the error if I dont set it to -1 in the inspector).
     
    qsjustin likes this.
  2. Superrodan

    Superrodan

    Joined:
    Nov 10, 2013
    Posts:
    10
    Here's kind of a hacky way to do this that I'm using with my dropdowns. I found that the problem I was having was that even changing the dropdown in code always triggered the OnValue Changed, which was not behavior that I wanted. I only wanted to trigger my code when the player actually changed the dropdown, NOT when I did.

    So, I'm using c# code to determine when the value changes and then I have a few extra checks to prevent unwanted behavior.

    So let's say you have a reference to your dropdown:
    public TMP_Dropdown DROPDOWNNAME;
    and a bool:
    public bool tempDisabled = false;

    In your Start() function on whatever the dropdown is changing you put something like this:
    Code (CSharp):
    1.  
    2. //This step here is optional, I just have a LOT of dropdowns and didn't want to set them all up in the inspector, so I grab them in start
    3. DROPDOWNNAME = this.GetComponent<TMP_Dropdown>();
    4. //THIS STEP IS THE IMPORTANT ONE
    5. DROPDOWNNAME.onValueChanged.AddListener(delegate { CheckifDropdownChanged(DROPDOWNNAME); });
    And then you create a function called:
    Code (CSharp):
    1.    void CheckifDropdownChanged(TMP_Dropdown DROPDOWNNAME)
    2.     {
    3.         if (tempDisabled == false)
    4.         {
    5.             // DO YOUR CODE HERE
    6.         }
    7.  
    8.     }
    Essentially, when you change a dropdown, either in the game OR in code it will trigger that function you created, so you hide your code behind a bool called tempDisabled.

    In most situations tempDisabled stays false so that your dropdown works normally. However, when you call whatever it is you are doing to reset the dropdown to -1 (which is causing your errors) you make sure to first set tempDisabled to true so that even if onValueChanged happens, the code doesn't run and cause your issues.

    Make sure after everything resets, to set tempDisabled back to false so that your dropdown will still function.
     
  3. Hexolenn

    Hexolenn

    Joined:
    Dec 27, 2021
    Posts:
    44
    The problem wasn't that I cant change the value without triggering my functions but I defenetly learned something from there.

    My problem is that I cannot select already selected dropdown menus. And because I cant do this I cannot use dropdown as a button without creating a new dropdown from scracth. So what I want is a way to select already selected dropdown items. In my question I said that to unselect any option I tried to change the "dropdown.value= -1" but that gave me an stackOverflow.

    And your code only stops codes starting multiple times because value is changed. The problem with changing the dropdown.value to -1 is that there cant be -1 indexed dropdown item so it gave me a stack overflow.

    So is there a way to set the dropdown.value to an useless or unseen item so that everytime I click it resests the dropdown.value to that useless item so that every one of my dropdown items can be selected again.

    To do this I tried to set my value to -1 which doesnt work.

    I tried to find how to find how long a dropdowns item list is so I can create an empty block at the end so that I can set it to that and not get any stackoverflow errors but I couldn't find anything that lets me see how many items are in a dropdown (I use multiple diffirent dropdowns as well so I cannot make it a static number). So I couldn't do this either.

    If there are other ways of making your dropdown unselect every option so I can use it as a button please tell me.
     
    Last edited: Apr 11, 2023
  4. qsjustin

    qsjustin

    Joined:
    Apr 21, 2014
    Posts:
    2
    I'm dealing with the same problem moments ago and I found the right way to do it. Quite simple:
    1. Set your dropdown's placeholder field with some empty image or text
    2. Then you can set the dropdown's value to -1 with code like this:
    Code (CSharp):
    1. dropdown.onValueChanged.AddListener(value => {
    2. // do your work
    3. dropdown.SetValueWithoutNotify(-1);
    4. });
     
  5. Hexolenn

    Hexolenn

    Joined:
    Dec 27, 2021
    Posts:
    44
    Thank You this works as I wanted it to