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 BadKarus · Feb 05, 2018 at 04:18 PM · c#coroutinewaitreturn value

Wait inside method until user action

Hello there! I need to wait until user click button and set some value. This value is needed in another class as shown below. I have no idea how to implement. I've tried to use coroutines but no matter what I do Method1 always returns value before it is set by user. I've also tried do..while but obviously it freezes program.

 class Class1{
     if(Class2.Method1())
         //do something
     else 
        //do something else
 }

 class Class2{ 
     bool Method1() {
         ShowDialog();
         WaitUntilUserSetValue();
         CloseDialog();
         return value;
     }
 }

Any help & tips are welcome!

Comment
Add comment · Show 7
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 TreyH · Feb 05, 2018 at 04:29 PM 0
Share

Are Class1 and Class2 both $$anonymous$$onobehaviours?

avatar image BadKarus · Feb 05, 2018 at 04:52 PM 0
Share

Yes, they are both $$anonymous$$onobehaviours.

avatar image TreyH · Feb 05, 2018 at 06:02 PM 0
Share

If you're waiting until the user has clicked a Button, have you tried assigning a function to that button? What does the button do right now?

avatar image BadKarus · Feb 05, 2018 at 06:10 PM 0
Share

Button is actually changing returning value. I want to create universal custom confirmation dialog box and use it at every destructive action.

Actually I have no idea how to wait for Button function to fire before executing the following lines of $$anonymous$$ethod1 (before returning proper value)

avatar image spokrock · Feb 05, 2018 at 06:30 PM 2
Share

Well it looks like you're going to have to get into call backs, delegates & some subclassing. Google semaphores this might help where you can delay execution and continue after some condition has been met.

Show more comments

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Timo326 · Feb 05, 2018 at 06:29 PM

Thank you for your further explanation in the comments below. I edited this answer depending on your information. Try it, i hope it will work, i haven't tested it.

 public class Class1 : MonoBehaviour {
 
     public GameObject confirmationDialog;       // To set in inspector
     public GameObject gameObjectToDelete;       // To set in inspector
 
 
     private bool clickedYes = false;
     private bool clickedNo = false;
 
     // Function is called if the object has du be deleted
     public void onDeleteClick()                 
     {
         StartCoroutine(deleteGameObject(gameObjectToDelete));
     }
 
     IEnumerator deleteGameObject(GameObject obj)
     {
         clickedNo = false;
         clickedYes = false;
         confirmationDialog.SetActive(true);
 
         while (clickedYes == false && clickedNo == false)
         {
             yield return 0;
         }
 
         confirmationDialog.SetActive(false);
         Destroy(gameObjectToDelete);            // ... or delete file ... or anything else
     }

     // function is called when button yes is clicked in the confirmation Dialog (Event)
     public void confirmationDialogClickedYes()          
     {
         clickedYes = true;        
     }


     // function is called when button no is clicked in the confirmation Dialog (Event)
     public void confirmationDialogClickedNo()           
     {
         clickedNo = true;
     }
 }



Comment
Add comment · Show 4 · 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 BadKarus · Feb 05, 2018 at 06:48 PM 0
Share

Well, but if I call $$anonymous$$ethod1 from another class as StartCoroutine, then how to wait until this coroutine ends (and proper Value is set by user) before actually using this Value in following lines of code?

avatar image Timo326 BadKarus · Feb 05, 2018 at 07:35 PM 1
Share

I think i did not understand your first question correctly. Well, now i think i understand what you are trying to get.

$$anonymous$$ake your dialog with an attached script (class2) and add an event (onClick) to the Buttons of your dialog. In the script set a bool ("clicked" for example) to true and set your value.

In an other gameobject (class1) check inside the Update() function if class2.clicked == true. And if this is done, read the value and execute your code.

Sorry, but i don't know how else i could explain this. Hope you get it!

avatar image BadKarus Timo326 · Feb 05, 2018 at 08:02 PM 1
Share

First of all thank you for your effort. You are explaining this really well and I get it. However unfortunately I cannot check if the user clicked desired buttons inside Update function. Actually I am trying to create simple universal confirmation dialog (with custom layout, texture etc.) which will show up before some critical actions like deletion. For example to delete Unit user needs to press Delete button, and I would like to onDeleteClick() method call the ConfirmationDialog (with some custom string) and based on its result finally delete or not the selected Unit. By universal I mean that the same instance of ConfirmationDialog (with some other string passed as parameter) could be also use for confirmation of quitting the game.

Show more comments
avatar image
-1

Answer by spokrock · Feb 05, 2018 at 09:08 PM

Have a look at this youtube channel:
Dialog System
https://www.youtube.com/watch?v=_nRzoTzeyxU

Channel https://www.youtube.com/user/Brackeys/videos

Comment
Add comment · 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
0

Answer by BadKarus · Feb 06, 2018 at 12:23 AM

Thanks to you I finally found working solution. Here is pseudocode. Maybe someday will be helpful for someone.

 class Class1 {
      void onButtonClicked(){
           Class2.Method1(Action SomethingToDo);
      }
      void SomethingToDo(){
           ...
      }
 }

 class Class2 {
      Action actionToDo;
      bool userClicked = false;
      bool confirmed;

      void Method1(action){
           actionToDo = action;
           ShowDialog();
           StartCoroutine(WaitForUserConfirm());
      }
      IEnumerator WaitForUserConfirm(){
           yield return new WaitUntil(() => userClicked);
           if(confirmed)
                actionToDo();
           CloseDialog();
      }
      void onConfirmButtonClicked(){
           userClicked = true;
           confirmed = true;
      }
      void onDeclineButtonClicked(){
           userClicked = true;
           confirmed = false;
      }
 }
Comment
Add comment · 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

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

441 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 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 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 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 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 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 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

Help with waiting / coroutine c# 1 Answer

C# Coroutine help 1 Answer

A simple Wait-Function without Coroutine (C#) 12 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 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