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.

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

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.

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