Question by
Lisk · Jan 27, 2017 at 07:50 PM ·
coroutines
How to wait for a function to complete before continuing?
I'm writing some test code which tests some network operations. I want to wait until an operation is finished before returning true or false (to signify whether the test passed or failed).
Below is what I have so far:
static bool ValidateLogin() {
bool success = true;
bool wait = true;
string playerA = "TestAccountA";
// Login
MatchmakerClientAPI.Instance.Login(playerA, (result) =>
{
DeveloperApi.Log("Logged in successfully");
wait = false;
}, (error) =>
{
DeveloperApi.LogWarning("Failed to login: " + error);
success = false;
});
// I want to wait until "Wait = false" before proceeding.
while (wait) ; // This currently causes Unity to hang
return success;
}
However, with the above code, Unity hangs. I suspect the answer will involve having to use coroutines, but I'm not sure how they apply to this situation.
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612105740im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Same Coroutine on multiple objects 1 Answer
using coroutine with fixedupdate and time. 0 Answers
Detect when a key is pressed or not pressed using Corutine? 0 Answers
What happens when calling StopCoroutine on a Coroutine that is awaited somewhere else ? 0 Answers
How to properly call Async Task 0 Answers