How to pass data between 2 yielding blocks of code?
In other environments, it is common to do something like:
getUsersMatchingCriteria() {
result = http_get(url, ...)
return result
}
anotherYieldingFunction() {
users = getUsersMatchingCriteria(...)
doStuffToUsers(users)
}
but I am having trouble figuring out how to do something similar in Unity. Yielding is done through coroutines, but coroutines can't return a result. After searching for an answer, I came across this answer, but that is an extremely elaborate workaround for something that seems like a common use case. I'm guessing there is a more-preferred way to do this in Unity, so instead of "how to return a value from a coroutine" I'm asking something more broad: how can I pass data between two pieces of yielding code? What is the Unity-esque standard for doing this?
My specific use case is that I have a function to roll a die -- it plays the rolling animation, and I want it to return the face-up value of the die when the animation completes. Another, also-yielding, function would use this when a user attacked a target to determine if their attack is a success.
The answer given right after the linked one is how I generally handle this: adding an System.Action
parameter that is called at the end of the coroutine.
Your answer
![](https://koobas.hobune.stream/wayback/20220612182817im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Checking Internet inside a Coroutine 0 Answers
Nothing after coroutine gets executed 1 Answer
[C#] How to write a generic yield return WaitForCondition with an expression as argument ? 3 Answers
IEnumerator skips remaining function after yield 1 Answer
Coroutine doesn't continue after yield another coroutine 0 Answers