- Home /
Question by
kowbonez · May 12, 2018 at 08:52 PM ·
random.rangedropdownoptions
Generate a random number in a range based on dropdown item
I want to generate a random number in a range whenever a dropdown item is selected.
if (Dropdown.option = "small")
Debug.Log(random.range(10,100))
else if (Dropdown.option = "moderate")
Debug.Log(random.range(101,500))
I am currently only able to generate a random number when the value in the dropdown is changed.
public class Random_Pop : MonoBehaviour {
Dropdown pop_Dropdown;
// Use this for initialization
void Start() {
pop_Dropdown = GetComponent<Dropdown>();
pop_Dropdown.onValueChanged.AddListener(delegate {
DropdownValueChanged(pop_Dropdown);
});
}
void DropdownValueChanged(Dropdown change) {
Debug.Log(Random.Range(50, 150));
}
}
Comment
Answer by kowbonez · May 12, 2018 at 09:19 PM
I found a quick solution to this, but it's not very elegant.
void Start() {
pop_Dropdown = GetComponent<Dropdown>();
pop_Dropdown.onValueChanged.AddListener(delegate {
DropdownValueChanged(pop_Dropdown);
});
}
void DropdownValueChanged(Dropdown change) {
if (pop_Dropdown.value == 0) {
Debug.Log("None Selected");
} else if (pop_Dropdown.value == 1) {
Debug.Log(Random.Range(10,50));
} else if (pop_Dropdown.value == 2) {
Debug.Log(Random.Range(45, 280));
} else if (pop_Dropdown.value == 3) {
Debug.Log(Random.Range(250, 755));
} else if (pop_Dropdown.value == 4) {
Debug.Log(Random.Range(735, 2500));
} else if (pop_Dropdown.value == 5) {
Debug.Log(Random.Range(1800, 8650));
}
//Debug.Log(Random.Range(50, 150));
}
Your answer
Follow this Question
Related Questions
How to loop through Dropdown options? 1 Answer
How to make filter with choose options 0 Answers
Dropdown button... 0 Answers
Dropdown extra options 1 Answer