How can I get a coroutine to return a value?

I have a coroutine to fade text in, and I think I just began to understand “yield return null” and how it exits the coroutine but saves the spot and continues the coroutine.

So far it works well, but I want a boolean to be returned when the text is done.

public void ActivateChapterDisplaySequence()
        Text chapterTitleTextTextCompRef = chapterCanvasRef.transform.Find("Chapter Title Text").gameObject.GetComponent<Text>();

        StartCoroutine(textFaderModRef.FadeTextIn(chapterTitleTextTextCompRef));  //This calls the coroutine in the other class.
public IEnumerator FadeTextIn(Text givenText)
        float textAlphaNum = 0;

        while (textAlphaNum <= 1.0f)
            textAlphaNum += Time.deltaTime / 3f;

            givenText.color = new Color(1.0f, 1.0f, 1.0f, textAlphaNum);

            yield return null;

I would like to somehow send the signal that the text is done fading with either a bool or some other way.

Does anyone have any ideas?


As far as I’m aware, the coroutine cannot return a boolean.

What you could do is to declare a boolean at the top of your code and set it to either true or false in your coroutine. In Update or somewhere else, you could check the variable. Or you could call a method that does something when your coroutine is done.

1 Like

I went with the “calling a function” option. I don’t want to have a ton of variables lying around and a function keeps things more organized.

I added one that triggers another coroutine and another since they’re fading timers, and then an “end signal” function that lets the game controller know it’s done.

Thanks for the help!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms