Question for CrossFadeAlpha

Hello! I just discovered this command (and it was relly usefull!). But I have a problem using it. Let me explain:

I did a script for use it for all my UI components and it is working perfectly for fade out (alpha 1 to 0). But if I want the reverse (alpha 0 to 1) it isn’t working if I disabled my UI element before (SetActive(false)).

The only way that I find to fix it was disable GraphicRaycaster after set alpha to 0, so UI element is there, but player can’t interact or see it. Of course I guess this isn’t a good way to do it if game have alot more UI elemnts, but was enough good for a simple project.

So the question is… is there a way to do a CrossFadeAlpha working from 0 to 1 alpha value just after set a UI element active with an alpha 0 (that it was set previously)?

Thanks so much ^^

    //=============================================

    /// <summary>
    /// Use this for check all Children or specific GameObject (remember use checkchildren to false for this feature).
    /// </summary>
    /// <param name="gameObject">Game object.</param>
    /// <param name="checkchildren">If set to <c>true</c> checkchildren.</param>
    /// <param name="newAlpha">0 for transparent, 255 opaque.</param>
    /// <param name="duration">Duration.</param>
    /// <param name="ignoreTimeScale">If set to <c>true</c> ignore time scale.</param>
    public void FadingItems(GameObject gameObject, bool checkchildren, float newAlpha, float duration, bool ignoreTimeScale, bool enableGameObject)
    {
 
        if (newAlpha < 0f)
            newAlpha = 0f;
        if (newAlpha > 1f)
            newAlpha = 1f;

        if (enableGameObject) {
            StartCoroutine (EnableGameObject (gameObject, 0f));
        } else {
            StartCoroutine (DisableGameObject (gameObject, duration + 0.01f));
        }

        if (checkchildren) {
            foreach (Transform reference in gameObject.GetComponentsInChildren<Transform>()) {             
                Fade(reference.gameObject, newAlpha, duration, ignoreTimeScale);
            }

        } else {
            Fade(gameObject, newAlpha, duration, ignoreTimeScale);
        }


        foreach (Transform reference in gameObject.GetComponentsInChildren<Transform>()) {
            if (reference.gameObject.GetComponent<Button> ()) {
                if (enableGameObject) {
                    reference.gameObject.GetComponent<Button> ().enabled = true;
                    reference.gameObject.GetComponent<Image> ().enabled = true;
                } else {
                    reference.gameObject.GetComponent<Button> ().enabled = false;
                    reference.gameObject.GetComponent<Image> ().enabled = false;
                }
            }
        }



    }
    //===========Fading or Unfading Objects=================================================================================

    private void Fade(GameObject reference, float newAlpha, float duration, bool ignoreTimeScale)
    {
        if (reference.gameObject.GetComponent<Text> ()) {
            reference.gameObject.GetComponent<Text> ().CrossFadeAlpha (newAlpha, duration, ignoreTimeScale);
        } else {
            if (reference.gameObject.GetComponent<Image> ()) {
                reference.gameObject.GetComponent<Image> ().CrossFadeAlpha (newAlpha, duration, ignoreTimeScale);
            }
        }

    }

    //===================================================================================================================

    IEnumerator DisableGameObject(GameObject reference, float delay)
    {
        yield return new WaitForSeconds (delay);
        //reference.gameObject.SetActive (false);
        try{
        reference.gameObject.GetComponentInChildren<GraphicRaycaster>().enabled = false;
        }catch{
        };
    }

    //===================================================================================================================

    IEnumerator EnableGameObject(GameObject reference, float delay)
    {
        yield return new WaitForSeconds (delay);
        //reference.gameObject.SetActive (true);
        try{
        reference.gameObject.GetComponentInChildren<GraphicRaycaster>().enabled = true;
        }catch{
        };

    }

CrossFadeAlpha() can’t have an initial alpha of zero. The function works by multiplying the values similar to a Lerp() function, so if you start with zero you’ll end up multiplying by zero and the function will always return zero. Try setting the graphic’s alpha to something really small (I’d suggest 0.00392156863, which is an alpha value of 1 in RGB-speak).

Also this belongs in the Scripting or UI forums, not the Getting Started one.

1 Like

Yeah, this is a known bug with CrossFadeAlpha (and a really annoying one, IMHO — it looks like it’s doing a Lerp, but it’s not).

@Taorcb 's work-around should work, though.

1 Like

Thanks @Taorcb and @JoeStrout . I’ll edit my script to follow the instructions.

Sorry for it. I am still very novice with Unity, so I thought that my forum is this one yet. I’ll post my questions in the correct forum next time.

Thanks again!

Actually, I think this was a fine Getting Started question. But Scripting or UI would have also been fine.

1 Like