CrossFadeAlpha help!

I am trying to fade in some text, fade it out, replace it, and fade in again. Unfortunately, it doesn’t fade out all the way, and then doesn’t attempt to fade in at all.

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class fadeIn : MonoBehaviour {
    public bool showWelcome = false;

    void Start () {
        gameObject.GetComponent<UnityEngine.UI.Text>().CrossFadeAlpha(0f, 0f, false);
    }
    void Update ()
    {
        if (showWelcome == true)
        {
            textIn();
            StartCoroutine(text2());
        }
    }

    void textIn ()
    {
        gameObject.GetComponent<UnityEngine.UI.Text>().CrossFadeAlpha(1f, 1f, false);
    }
    void textOut ()
    {
        gameObject.GetComponent<UnityEngine.UI.Text>().CrossFadeAlpha(0f, 1f, false);
    }

    IEnumerator text2()
    {
        yield return new WaitForSeconds(5);
        textOut();
        yield return new WaitForSeconds(5);
        gameObject.GetComponent<UnityEngine.UI.Text>().text = ("This is a custom UI for Windows Desktop");
        textIn();
    }
}

Unrelated to you problem, but let’s reduce the wordiness of this script:

  1. Put ‘using UnityEngine.UI’ at the top with the other statements, so you can just do GetComponent()
  2. Cache GetComponent() so you don’t have to do that all the time:
Text textObject;
void Start() {
textObject = GetComponent<Text>();
text.CrossFadeAlpha(0f, 0f, false);
}

As for the problem - is it possible something else is mucking with the time scale?

Put in a Debug.Log(“here”) in various places to ensure that your functions are being called when you expect them to be.

Thanks I will try that

EDIT:
Thanks to your suggestion, I have figured out that it is running it multiple times per second?

You’re starting the coroutine from Update, so it’ll start a new copy of the coroutine every frame. It looks like you should probably be setting showWelcome to false when you start the coroutine?

Thanks
EDIT: tested and working!!!