Search Unity

Question CrossFadeAlpha Help

Discussion in 'Scripting' started by unity_1j7X4B9xCQ0klw, Jul 2, 2020.

  1. unity_1j7X4B9xCQ0klw

    unity_1j7X4B9xCQ0klw

    Joined:
    Jun 30, 2020
    Posts:
    14
    Hey coders, I'm a beginner at C# and having a little trouble with the CrossFadeAlpha method on an image. It seems really straight forward but it's just not doing what I expected. I want my chosen image to fade in over 3 seconds. Here's the code I have:


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

    public class UIFader : MonoBehaviour
    {
    public Image myim;

    // Update is called once per frame
    void Update()
    {
    myim.CrossFadeAlpha(1, 3.0f, false);
    }
    }


    What's going wrong here? Thanks so much in advance!
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Update() runs every frame. So you're calling CrossFadeAlpha every frame. I assume that will restart the crossfade every frame, thus not really visibly doing anything. Maybe you want to put that call in Start()? Start() runs once at the beginning of the object's lifecycle.
     
  3. unity_1j7X4B9xCQ0klw

    unity_1j7X4B9xCQ0klw

    Joined:
    Jun 30, 2020
    Posts:
    14
    That could be an issue, but it's not working within Start either haha. I just tried changing that. Perhaps I had more than one issue with it, but it's still just leaving the image totally transparent and doesn't seem to change the alpha at all.

    The code is now:


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

    public class UIFader : MonoBehaviour
    {
    public Image myim;

    void Start()
    {
    myim.CrossFadeAlpha(1, 3.0f, false);
    }
    }
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Do you have any errors in the console?
     
  5. unity_1j7X4B9xCQ0klw

    unity_1j7X4B9xCQ0klw

    Joined:
    Jun 30, 2020
    Posts:
    14
    Nah the console is empty.
     
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Throw a Debug.Log() in there to make sure your code is actually running:

    Code (CSharp):
    1. void Start() {
    2.   Debug.Log("MY CODE IS RUNNING");
    3.   myim.CrossFadeAlpha(1, 3.0f, false);
    4. }
     
  7. unity_1j7X4B9xCQ0klw

    unity_1j7X4B9xCQ0klw

    Joined:
    Jun 30, 2020
    Posts:
    14
    I just figured it out, CrossFadeAlpha changes the alpha of the canvas renderer, not the image. This code works:


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

    public class UIFader : MonoBehaviour
    {
    public Image myim;
    public float fadeSpeed;

    void Start()
    {
    myim.canvasRenderer.SetAlpha(0);
    myim.CrossFadeAlpha(1.0f, fadeSpeed, false);
    }
    }
     
    akuno, jwcowand and PraetorBlue like this.
  8. unity_1j7X4B9xCQ0klw

    unity_1j7X4B9xCQ0klw

    Joined:
    Jun 30, 2020
    Posts:
    14
    I'll definitely keep this move in my back pocket for the future :) I'm sure to run into a billion issues like this. Thanks for helping out!