- Home /
Drop down menu not working how I expected
I have a drop down menu that has a public void with an int variable. This allows you to select an option from the drop down menu during runtime and produce a function (in this case language selection). I assign the OnClick function as a Dynamic int in the Inspector.
However, I now want to add the same function to an Event Trigger (EndDrag) but when I try to assign the function, I only get Static Parameters, no dynamic ones.
Can anyone tell me how to access the Dynamic int?
My script:
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class DropDownMenu : MonoBehaviour {
public Dropdown myDropdown;
public static string key = null;
public CanvasGroup ok;
void Start() {
myDropdown.onValueChanged.AddListener(delegate {});
ok.alpha = 0f;
ok.blocksRaycasts = false;
}
void Destroy() {
myDropdown.onValueChanged.RemoveAllListeners();
}
public void SetDropdownIndex(int index) {
myDropdown.value = index;
if(index == 1){
key = "English";
Debug.Log ("English");
TextLocalization.SelectLanguage("English");
ok.alpha = 1f;
ok.blocksRaycasts = true;
}
if(index == 2){
key = "German";
Debug.Log ("German");
TextLocalization.SelectLanguage("German");
ok.alpha = 1f;
ok.blocksRaycasts = true;
}
}
}
Answer by fafase · Aug 30, 2016 at 01:44 PM
You need to update your parameter to receive a MonoBehaviour type reference. I assume your dynamic value comes from a script then you pass that script as parameter to the listener. When the listener is called, it will go to the script and find the value at that moment:
public class DataStorage:MonoBehaviour{
private int index;
public int Index{ get{ return this.index; } }
}
public class DropDownMenu : MonoBehaviour
{
public void SetDropdownIndex(DataStorage data) {
myDropdown.value = data.Index;
if(data.Index == 1){
}
if(data.Index == 2){
}
}
}
Thanks for the reply. I tried adding this scripting to the 'End Drag' Event Trigger but unfortunately it references the default index in the Inspector which is 0. I want it to work in the exact same way as the On Value Changed but just with End Drag ins$$anonymous$$d.