Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Nanashi91 · Jan 14, 2013 at 11:41 AM · c#coroutineyield

C# Wait for Coroutine

Hello. I send a request to my server and want to wait for an answer before continuing my function. My problem is my algorithm is not waiting in any way. It is just ignoring my Coroutine and my while function. My structure:

 IEnumerator startGame(){ //<--- is a started coroutine
   //... code for doing some stuff
   getCard();
   //--- other code

 }

 void getCard(){
   //..Code
   SendRequest();

   startWait();
   //..more Code
 }

 IEnumerator startWait(){
     Debug.Log("start bla");
     yield return StartCoroutine ("bla");
     Debug.Log("bla finished");
     
 }
 //bla is checking if the stuff I receive from the server is fitting
 //the one I am waiting for. if it is fitting break the while loop
 IEnumerator bla(){
         Debug.Log ("Start waiting");
         waitForReply = true;
         while(waitForReply){
             //code for checking my received strings....
             yield return null;
         }
         Debug.Log ("Done");
     }

First I tried to wait for my reply in the startWait-Function but to see how deep he is going down in this algorithm I used one more function.

It is running into startWait. But in the moment when it is in startWait it is leaving startWait already without even reaching my Debug.Log.

Does anyone know what I am doing wrong or is there an even better way to do it?

Any idea or hint can be helpful. Thank you in advance.

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

3 Replies

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

Answer by robhuhn · Jan 14, 2013 at 01:38 PM

I have not tested it but as far as I understand you want the following:

 void GetCard()
 {
      //get card start
      StartCoroutine("WaitForReply"); //first to upper is a C# convention
      //get card end
 }

 //bla is checking if the stuff I receive from the server is fitting
 //the one I am waiting for. if it is fitting break the while loop
 IEnumerator WaitForReply()
 {
        Debug.Log ("Start waiting");
        waitForReply = true;
        while(waitForReply){
             //code for checking my received strings....
          yield return null;
        }
        ReplyReceived();
     }

 void ReplyReceived()
 {
        Debug.Log ("Done");
        //more code
 }



@edit

This is the order of the logs if WaitForReply takes some time:

  • get card start

  • Start waiting

  • get card end

  • Done

In C# you can also call it this way

 StartCoroutine(WaitForReply());

But then you don't have no access through StopCoroutine("name") anymore.

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
1

Answer by PAEvenson · Jan 14, 2013 at 12:25 PM

It doesnt look like you are calling StartCoroutine(startWait()); instead it looks like you are just calling it like a function.

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 Nanashi91 · Jan 14, 2013 at 12:36 PM 0
Share

I want to call startWait like a normal function and inside startWait I want to call a Coroutine and wait for it.

If I understand right my algorithm should run into startWait(). Afterwards it will start bla() as a Coroutine and wait for it and after the Coroutine bla is finished it will continue the function startWait until the end. After startWait is finished as well it will continue my getCard-Function.

At least I thought it should work like that.

avatar image PAEvenson · Jan 14, 2013 at 01:42 PM 1
Share

Yield must be inside a coroutine, otherwise, it will not wait.

avatar image
0

Answer by Filip Van Bouwel · Jan 14, 2013 at 02:15 PM

Are you calling those functions inside a Monobehaviour? It won't work in a class.

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

11 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

Related Questions

Trouble Resuming after Yielding while Inside Coroutine 1 Answer

Multiple Cars not working 1 Answer

Waiting twice inside coroutine (C#) 2 Answers

Coroutine execution not continuing [Solved] 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