- Home /
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.
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.
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.
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.
Yield must be inside a coroutine, otherwise, it will not wait.
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613090923im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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