I want to wait for the user select an option from a Dropdown. I am prototyping a turn based battle and using the Dropdown to give a list of actions like “Attack”, "Block’, etc. I want to block anything from happening until the user selects an action. I have tried to introduce a boolean variable that changes in OnValueChanged, but I don’t know how to block other than while(!valueNotChanged) {}, which causes an infinite loop and I have to close Unity. Here is what I’m talking about:
public class Encounter : MonoBehaviour {
[SerializeField]
public Dropdown dropDown;
private bool choiceMade;
// Use this for initialization
void Start ()
{
choiceMade = false;
dropDown = GameObject.Find ("/GUI/TopicList").GetComponent<Dropdown> ();
dropDown.onValueChanged.AddListener(DropdownValueChanged);
}
private void DropdownValueChanged(int choice)
{
choiceMade = true;
}
public void go()
{
// Populate the Dropdown
dropDown.ClearOptions ();
dropDown.AddOptions (player.GetTopicStrings());
// Set to false
choiceMade = false;
// while the enemy is not dead
while(enemy.hp > 0)
{
while(!choiceMade)
{
Debug.Log ("Waiting for choice");
}
// Get the choice from the Dropdown
int choice = dropDown.value;
// Do stuff depending on choice made by player
Is there a way to achieve what I want, or do I need to switch from using Dropdowns entirely?