Fading....again!!

Hi all,

I know this has been asked millions of times (I’ve found all the threads lol) but I’m having trouble getting it working. I’ve made the Texture object in the Hieracgy, I’ve made an empty game object and placed the following script into it hooking it up to the texture object. I just can’t get it to work. I’m getting an error on the StartCoRoutine section but can’t see how to get it working.

using UnityEngine;
using System.Collections;

public class Fade : MonoBehaviour
{
    public Texture2D guiObject;
    public float fadeTime = 1.0f;

    private float alpha = 0.0f;

    enum fadeDirection
    {
        In,
        Out
    }

    void OnGUI()
    {
        GUI.color = new Color(1.0f, 1.0f, 1.0f, alpha);
        GUI.DrawTexture(new Rect(Screen.width / 2, Screen.height / 2, guiObject.width, guiObject.height), guiObject);
        GUI.depth = 1;
    }

    IEnumerator Start()
    {
       yield return StartCoroutine("FadeGUITexture(fadeTime, fadeDirection.In)");
       yield return new WaitForSeconds(2.0f);
       yield return StartCoroutine("FadeGUITexture(fadeTime, fadeDirection.Out)");
    }

    IEnumerator FadeGUITexture(float timer, fadeDirection fadeType)
    {
        var start = fadeType == fadeDirection.In ? 0.0f : 1.0f;
        var end = fadeType == fadeDirection.Out ? 1.0f : 0.0f;
        var i = 0.0f;
        var step = 1.0f / timer;

        while (i < 1.0f)
        {
            i += step * Time.deltaTime;
            alpha = Mathf.Lerp(start, end, i) * 0.5f;
            yield return 0;
        }
    }
}

Any Ideas guys?

See the docs for StartCoroutine.

–Eric

No, you need to use yield. His script is correct, except the exact syntax for StartCoroutine isn’t right.

–Eric

No. The second example is not wrong, your first example is wrong. For one, it’s StartCoroutine, not StartCorouting, and second, like I said, it should use yield. Otherwise it won’t wait until the coroutine is done. (Neither example should use quotes though.)

–Eric

So to answer your question your script should look like this.

using UnityEngine;
using System.Collections;

public class Fade : MonoBehaviour
{
    public Texture2D guiObject;
    public float fadeTime = 1.0f;

    private float alpha = 0.0f;

    enum fadeDirection
    {
        In,
        Out
    }

    void OnGUI()
    {
        GUI.color = new Color(1.0f, 1.0f, 1.0f, alpha);
        GUI.DrawTexture(new Rect(Screen.width / 2, Screen.height / 2, guiObject.width, guiObject.height), guiObject);
        GUI.depth = 1;
    }

    IEnumerator Start()
    {
       yield return StartCoroutine( FadeGUITexture(fadeTime, fadeDirection.In) );
       yield return new WaitForSeconds(2.0f);
       yield return StartCoroutine( FadeGUITexture(fadeTime, fadeDirection.Out) );
    }

    IEnumerator FadeGUITexture(float timer, fadeDirection fadeType)
    {
        var start = fadeType == fadeDirection.In ? 0.0f : 1.0f;
        var end = fadeType == fadeDirection.Out ? 1.0f : 0.0f;
        var i = 0.0f;
        var step = 1.0f / timer;

        while (i < 1.0f)
        {
            i += step * Time.deltaTime;
            alpha = Mathf.Lerp(start, end, i) * 0.5f;
            yield return 0;
        }
    }
}

BTW if you want to load a level in where it says the wait for seconds part instead slip in this at the begining of the Start function

DontDestroyOnLoad(gameObject);

and then where it has the 2 second wait replace that line with these:

Application.LoadLevel(levelNameHere);
yield return new WaitForEndOfFrame();
yield return new WaitForEndOfFrame();

and then at the end of the Start function
Destroy(gameObject); to clean it out.

waiting two frames is ideal so your awakes and starts can get called properly before fading out. you may find you need more which really wouldnt hurt to have 4 since a frame should be fairly quick.

Also, Your GUI.depth at 1 is after the draw itself putting the draw farther back, you should try using like -100 to make sure its going to draw ontop of everything and before the line to DrawTexture.

Okay sorry. Thanks for telling me. I got it now. I always thought yield cannot be used with startcoroutine. Really sorry.

Yeah, yield is a very important and useful thing to use when starting coroutines.

–Eric

What exactly is the benefit of using StartCoroutine() as opposed to just yielding a function? In my own code, I generally just run coroutines like so:

function foo1() {
     yield foo2();
     DoSomethingElse();
}

function foo2() : boolean{
     DoSomething();
     return true;
}

Is there a difference between the above and inserting StartCoroutine()?

We’re talking about C# here, where using StartCoroutine is necessary.

–Eric