Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by sterlingm · Jul 25, 2017 at 12:41 AM · dropdownblock

Block until Dropdown value has changed?

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Brogan89 · Jul 25, 2017 at 03:47 AM

Dropdowns are event based, so nothing will be triggered until the user selected an option. This means you don't need "wait" for the user...

If i understand what you're trying to do is you want to option to always be "Block" unless the user picks something different?

If so, there a few ways of doing this... based on you code you have now I'm guessing you are calling go() from another script. So what you should do is add a callback which will be invoked once dropdown option has changed...

Maybe try this

  public void go(UnityAction<int> callback)
     {
         // Populate the Dropdown
         dropDown.ClearOptions();
         dropDown.AddOptions(player.GetTopicStrings());
         dropDown.onValueChanged.AddListener(callback);
     }

then from your other script you would go like this

 encounter.go(choice => 
 {
 //... whatever you want to do with that choice
 });

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sterlingm · Jul 25, 2017 at 02:24 PM 0
Share

Thanks for your suggestions! "nothing will be triggered until the user selected an option. This means you don't need "wait" for the user". If I don't "wait", then it just takes whatever value is selected in the Dropdown, and proceeds with my while loop. I want to wait for a new value to be selected so that 1) it feels like a turn and 2) it gives the user some time to think about what they want to select. Your approach seems like it would only trigger if the user selects something, but the user is free to do other things in the meantime. What I want is for the user to be "locked in" to selecting something from the dropdown.

avatar image Brogan89 sterlingm · Jul 25, 2017 at 10:39 PM 0
Share

Creating a "wait" function in Unity can only really be done in a Coroutine or in a polling situation in the Update function.

Calling Coroutines from outside the script can be a little long winded, so you could do something like this:

         public void go()
         {
             // Populate the Dropdown
             dropDown.ClearOptions();
             dropDown.AddOptions(player.GetTopicStrings());
 
             StartCoroutine(Go());
         }
 
         private IEnumerator Go()
         {
             // Set to false
             choice$$anonymous$$ade = false;
 
             // while the enemy is not dead
             while (enemy.hp > 0)
             {
                 while (!choice$$anonymous$$ade)
                 {
                     Debug.Log("Waiting for choice");
                     yield return null;
                 }
 
                 // Get the choice from the Dropdown
                 int choice = dropDown.value;
                 // Do stuff depending on choice made by player
 
                 yield return null;
             }
         }

which you still calling go() but it off loads to a coroutine. you're script will then continuously be looping within while(!choice$$anonymous$$ade) coroutines allow you to do this without making the game hang or even crash... Unity doesn't really like while loops, so its best to put it in coroutine, its kind of like another thread, but not exactly.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

68 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Drop down list to inspector 2 Answers

Make use of a button in a drop down menu? 0 Answers

When accessing the Add Component drop-down menu, the following display issue occurs. How can I resolve this? 0 Answers

Getting information of which Dropdown menu has been changed 0 Answers

How to make dynamic dropdown to choose items in an Array? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges