Assitance with "breathing" font size (increasing and decreasing using coroutines)

I'm trying to make the font of my 'Start Game' button "breath", increasing and decreasing in size using Coroutines.

Font size starts with 2.0, onStart it increases to 2.20 (progresively by +0.01 till it reaches 2.20) and then drops back to 2.0 (progresively by -0.01 till it reaches 2.0) and then repeat.

My code works for the first part perfectly where it increase all the way to 2.20 but for some reason it is not decreasing back to 2.0 from 2.20. Does anyone know what i'm doing wrong?

public class Font_Breathing : MonoBehaviour {

public TMP_Text startGame;
private float change = 0.01f;
private float delay = 0.0f;

void Start()
{
    StartCoroutine(IncreaseFont());
}

void Update()
{

}

IEnumerator IncreaseFont()
{

    while (startGame.fontSize >= 2.0f)
    {
        yield return new WaitForSeconds(delay);

        startGame.fontSize += change;

        if (startGame.fontSize >= 2.20f)
        {
            StartCoroutine(DecreaseFont());
        }
    }

}

IEnumerator DecreaseFont()
{
    while (startGame.fontSize >= 2.20f)
    {
        yield return new WaitForSeconds(delay);

        startGame.fontSize -= change;

        if (startGame.fontSize <= 2.0f)
        {
            StartCoroutine(IncreaseFont());
        }
    }

}

}



Read more here: https://stackoverflow.com/questions/64419416/assitance-with-breathing-font-size-increasing-and-decreasing-using-coroutines

Content Attribution

This content was originally published by Ram Killnani at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: