Search Unity

Reading Dropdown.values problem

Discussion in 'Getting Started' started by Shin_Toasty, Jan 23, 2018.

  1. Shin_Toasty

    Shin_Toasty

    Joined:
    Jun 15, 2017
    Posts:
    48
    Hello again

    the commented out line below doesn't work - but the if statement does. Why? I don't want to write an if statement for every dropdown option.

    Code (CSharp):
    1.    
    2.   void Update() {
    3.  
    4.  //       FoyerController.roundsToWin = (Rounds.value + 1);
    5.  
    6.        if (Rounds.value == 1) {
    7.             FoyerController.roundsToWin = 2;
    8.        }
    9.  
    10.     }    
    No errors in the console.

    Also: do I have to constantly monitor dropdowns within an Update function to use them?

    Overall It seems I'd be better off with a panel with normal buttons inside it; less hassle.
     
  2. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    Please do not do it this way!

    Check out the manual for Dropdowns. Notice the section for On Value Changed. With UI elements, you want to tie into these events and call your own functions. So you'd define something like the following method:
    Code (CSharp):
    1. void DropdownChange(int index) {
    2.     FoyerController.roundsToWin = int.Parse(myDropdown.options[index].text);
    3. }
    in one of your GameObjects. Click the + button in the On Value Changed section of your dropdown, drag the GameObject to the slot that was created, and pick your method from the dropdown list. Notice that the method gets the index of the selected option, so you'd use that to get the option from the list, then parse it to an int to be able to actually assign it.

    It's worth noting that if you're working with the UI system introduced in 4.6 (anything in the UnityEngine.UI namespace), you should be using these events (or binding delegates, but that's a bit more complicated) and not checking them inside Update.
     
    JoeStrout and Shin_Toasty like this.
  3. Shin_Toasty

    Shin_Toasty

    Joined:
    Jun 15, 2017
    Posts:
    48
    I just needed to add 'public' before void and your code worked thanks. Using functions in my own script on OnValueChanged is much better.

    BTW I did RTFM: my code is based on this: https://docs.unity3d.com/ScriptReference/UI.Dropdown-value.html - it uses Update. So if Unity's own Scripting IPA tells me to do it the wrong way what chance have I got? :mad: It did seem stupid and wasteful to be checking a simple dropdown many times per second. :(

    I would still like to know why the if statements worked but FoyerController.roundsToWin = (Rounds.value + 1); didn't.

    Binding delegates seems overly complicated here - what's the advantage?
     
    Schneider21 likes this.
  4. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    Awesome! Glad to hear it! And thanks for posting back indicating as much!

    Ew. I hadn't seen that page. I have no idea why that example exists. Must be some old holdover code, still. I guess it has the advantage of not requiring a screenshot of the Inspector, but still...

    My best guess would be that the value of Rounds was never 1. Maybe it was "1" as a string or something. I can't really say without seeing it in more detail.

    Delegates are awesome. If you're setting up a lot of stuff through code, they're a great way to do event binding. At the stage you're probably at right now, though, doing it the way you have it working is probably best. Only look into more complicated options once you find yourself needing a more complex solution!
     
    Shin_Toasty likes this.
  5. Shin_Toasty

    Shin_Toasty

    Joined:
    Jun 15, 2017
    Posts:
    48
    In FoyerController.cs, the line was:
    Code (CSharp):
    1.     public static int roundsToWin = 1;
    nothing special, that was all. The option I chose in the inspector OnValueChanged was Dropdown > Value, not one of my own functions.

    Also: what if the options on my dropdown were strings and I therefore needed to ignore them and just use the option value, no parsing required?
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Just use the index + 1 in the callback if they're all in order as you want? :)
     
    Shin_Toasty likes this.
  7. Shin_Toasty

    Shin_Toasty

    Joined:
    Jun 15, 2017
    Posts:
    48
    I found that if I attached a script to a dropdown with the simple declaration

    Code (CSharp):
    1. public static Dropdown LevelNumber;
    or whatever that then enabled me to ask things like:

    Code (CSharp):
    1. if (FoyerDropdowns.LevelNumber.value == 1)
    in my main GameController.cs, or in any other script in the scene, which works great. The dropdown itself is simply set to Dropdown.value on Value Changed (Int32).