Search Unity

[5.2]Dropdown , OnValueChanged.

Discussion in 'UGUI & TextMesh Pro' started by Testfor, Sep 10, 2015.

  1. Testfor

    Testfor

    Joined:
    Jan 22, 2015
    Posts:
    55
    Hey guys:

    I have been trying to set up the new dropdown element but I cant make it work as it should:

    I am using this script to take the "value" from the dropdown:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. using System.Collections;
    5.  
    6. public class RocketController : MonoBehaviour
    7. {
    8.     //Canvas
    9.     [SerializeField]private Dropdown engineDropD;
    10.  
    11.     //Elements
    12.     private ItemDb.Item engine;
    13.     private ItemDb.Item body;
    14.     private ItemDb.Item nouse;
    15.  
    16.     //Items db
    17.     [SerializeField]private ItemDb itemDb;
    18.  
    19.     //Internal vars
    20.     private int curEngine = 0;
    21.  
    22.  
    23.     void Update()
    24.     {
    25.         curEngine = engineDropD.value;
    26.         Debug.Log(curEngine);
    27.     }
    28. }
    29.  
    I dont think that is efficient at all , I tried to make a public function with an int as parameter and attach it to the OnValueChanged but it always give me a value of 0.

    Any suggestions?

    See you.
     
    tiddles451 likes this.
  2. phil-Unity

    phil-Unity

    Unity UI Lead Developer

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    So it works for me. Both using the .value and listening to the event. With the event make sure you select the dynamic type not the static.
     
    tianpengji likes this.
  3. Testfor

    Testfor

    Joined:
    Jan 22, 2015
    Posts:
    55
    Thanks for the reply.

    Well I tried again making a public void:

    Code (csharp):
    1.  
    2. public void SetEngine(int id)
    3.     {
    4.         Debug.Log(id);
    5.         engine = itemDb.items[id];
    6.     }
    7.  
    And assigned in the event:

    But it keeps giving 0.

    What do you mean with the dynamic type?

    Thanks!
     
  4. phil-Unity

    phil-Unity

    Unity UI Lead Developer

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    in the On value changed in the inspector. Where you have RocketController.SetEngine. You see how below that you have a field that has a 0. That means that you've told the event system to use a static value (in this case 0).

    If you open that drop down again you see some heading "Dynamic parameters" followed by allowed functions (ones that accept the correct parameter being passed) then "Static parameters" followed by functions that accept any sort of parameter (could be strings or anything as the passed value would be the one specified).

    You need to select you SetEngine to be from the dynamic list. If done correctly the field with the 0 will disappear as the value is now being set via the dropdown
     
  5. Testfor

    Testfor

    Joined:
    Jan 22, 2015
    Posts:
    55
    Ok I am stupid!

    I saw that a few hours ago with the slider tutorial and just ignored it. Thanks a lot!
     
  6. steunity2

    steunity2

    Joined:
    Jun 16, 2015
    Posts:
    8
    Here! I'm another stupid!
     
    phil-Unity likes this.
  7. phuocnb

    phuocnb

    Joined:
    Nov 20, 2015
    Posts:
    2
    Look at my screenshots to get to understand :)
     

    Attached Files:

    lclemens, H0163R, mcroswell and 6 others like this.
  8. anuj

    anuj

    Joined:
    Jun 15, 2015
    Posts:
    28
    or you can assign the same thing dynamically from script :

    Code (CSharp):
    1.  public Dropdown myDropdown; // declare your dropdown
    2.  
    3.    void Start()
    4.     {
    5.    
    6.  
    7.       myDropdown.onValueChanged.AddListener(delegate {
    8.             OnMyValueChange(myDropdown);
    9.         });
    10.      }
    11. public void OnMyValueChange(Dropdown dd)
    12. {
    13. Debug.Log(dd.value);
    14. }
    15.  
    16. void OnDestroy()
    17.   {
    18.   myDropdown.onValueChanged.RemoveAllListeners();
    19.  
    20.   }
    21.  
    22.  
     
    qqqbbb and AzatKhafizov like this.
  9. JJunior

    JJunior

    Joined:
    May 22, 2019
    Posts:
    53
    2022 and still doing 2016 mistakes :D