• 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 ddk4113 · Jan 18, 2018 at 10:10 AM · functioncoroutinesthreadingwait

How to make a function wait untill a specific bool is true?

Here's a piece of code that i want to execute but how can i wait for a specific bool to be true inside a function


 bool one = false;
 bool two = false;
 public bool A(){
     if (B())
         return true;
     else 
         return false;        
 }
 private bool B(){
     one = false;//1
     StartCoroutine (C ());//2
     // I need to wait until bool two is true and then return one
     yield return one;//5
 
 }
 private IEnumerator C (){
     two = false;//3
     yield return new WaitForSeconds (1f);
     if(somet$$anonymous$$ng is true)
         one = true;
     else
         one = false;
     yield return one;
     two = true;//4
 
 }



My current execution order is 1,2,5,3,4 but i need the execution order as 1,2,3,4,5. Please Help??

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Hellium · Jan 18, 2018 at 10:53 AM

Further to your real code, here is how I would do it. I haven't tested the following code though.

 #region Checking For Internet
 public void CheckForInternet( System.Action onInternetAvailable, System.Action onInternetUnavailable )
 {
     if( !IsInternetReachable() )
     {
         if( onInternetUnavailable != null )
             onInternetUnavailable();
         return ;
     }
     
     StartCoroutine( CheckForInternetConnectivityIEnum( onInternetAvailable, onInternetUnavailable ) ) ;
 }
 
 private bool IsInternetReachable()
 {
     return Application.internetReachability == NetworkReachability.ReachableViaCarrierDataNetwork || Application.internetReachability == NetworkReachability.ReachableViaLocalAreaNetwork ;
 }
 
 System.Collections.IEnumerator CheckForInternetConnectivityIEnum(System.Action onInternetAvailable, System.Action onInternetUnavailable)
 {
     WWW www = new WWW ("some api w$$anonymous$$ch gives sucess 1");
     yield return www;
     if ( www.error == null )
     {
         JsonData jsonDat = JsonMapper.ToObject (www.text);
         Debug.Log( "Login API success = " + jsonDat["success"] );
         if ( jsonDat["success"].ToString() == "1" )
         {
             if( onInternetAvailable != null )
                 onInternetAvailable();
         }
         else
         {
             if( onInternetUnavailable != null )
                 onInternetUnavailable();
         }
     }
     else if( onInternetUnavailable != null )
         onInternetUnavailable();
 }
 #endregion


And call it as follow :

 public void Foo()
 {
     CheckForInternet( OnInternetAvailable, OnInternetUnavailable ) ;
 }
 public void OnInternetAvailable()
 {
     Debug.Log("Internet is available");
 }
 public void OnInternetUnavailable ()
 {
     Debug.LogWarning("Internet is unavailable");
 }


INITIAL ANSWER

I don't t$$anonymous$$nk what you are trying to ac$$anonymous$$eve is possible without more coroutines and callbacks. Here is my suggestion:

     bool one = false;
     bool two = false;
     public void A( System.Action<bool> onResultFetched )
     {
         StartCoroutine( B( onResultFetched ) );
     }
     private System.Collections.IEnumerator B( System.Action<bool> onResultFetched )
     {
         one = false;
         yield return StartCoroutine( C() );
         onResultFetched.Invoke( one );
     }
     private System.Collections.IEnumerator C()
     {
         two = false;
         yield return new WaitForSeconds( 1f );
         if ( somet$$anonymous$$ng is true )
             one = true;
         else
             one = false;
         two = true;
     }



And then, you call A with a callback:

     private void Test()
     {
         A( ( result ) => Debug.Log( result ) );
     }

Comment
Add comment · Show 10 · 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 ddk4113 · Jan 18, 2018 at 11:29 AM 0
Share
avatar image Hellium ddk4113 · Jan 18, 2018 at 11:39 AM 0
Share
avatar image ddk4113 Hellium · Jan 18, 2018 at 12:21 PM 0
Share
avatar image ddk4113 · Jan 18, 2018 at 12:24 PM 0
Share
avatar image Hellium ddk4113 · Jan 18, 2018 at 02:17 PM 0
Share
avatar image ddk4113 · Jan 19, 2018 at 07:41 AM 0
Share
avatar image Hellium ddk4113 · Jan 19, 2018 at 08:07 AM 0
Share
avatar image ddk4113 Hellium · Jan 19, 2018 at 09:33 AM 0
Share
Show more comments
avatar image
2

Answer by andreim44 · Jan 18, 2018 at 10:15 AM

I t$$anonymous$$nk you're looking for

yield return new WaitUntil( () => B() );

// where B is your boolean method.

In your case, use

yield return new WaitUntil( () => two == 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 ddk4113 · Jan 18, 2018 at 10:19 AM 0
Share
avatar image andreim44 ddk4113 · Jan 19, 2018 at 08:23 AM 0
Share
avatar image ddk4113 andreim44 · Jan 19, 2018 at 09:30 AM 0
Share
Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

75 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

Related Questions

Alternative for semaphores in Unity? 2 Answers

need to wait for a few seconds before running it again... 1 Answer

Return value from coroutine to non monobehaviour 1 Answer

Unity3D and C# - Coroutines vs threading 4 Answers

Make a function that can return string or IEnumerator 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