- Home /
How to return my variable from the coroutine ??
First time in Unity, tutorials have gotten me this far.
How can I return the data variable from the coroutine to a variable in the validate subroutine? I've tried returning, making it a global variable, callbacks, I can't seem to make it work....... pls help
Answer by Bunny83 · Jan 21, 2021 at 03:32 AM
You can not "return" anything to the caller (or better starter) of a coroutine as the coroutine is a statemachine object which outlives the caller. The StartCoroutine call will return before the coroutine has finished. So the code the follows the StartCoroutine call would be executed long before the data you are interested in has arrived. You have to handle the data either inside the coroutine once it'S there, or when you use a callback, you can delegate the handling to that callback. There are no other ways around that. You can not (and should not) make synchronous web requests as they would have to block the main thread for the time it takes to finish the request.