Alternative for the fade-in script - any comments?

So I came up with an alternative for the FadeIn script and I simply want to what you guys think of it.

public float fadeInTime;

private Image panelImg;
private Color panelColor = Color.black;

void Start ()
{
    panelImg = GetComponent<Image>();
    FadeIn();
}

void FadeIn()
{
    panelColor.a -= Mathf.Clamp01(Time.deltaTime / fadeInTime);
    if ((panelImg.color = panelColor).a > 0) Invoke("FadeIn", 0.000001f);
    else Destroy(gameObject);
}

As you can see I am using Mathf.Clamp01 to obtain a value between 0 and 1 that is relative to the Delta Time and the desired FadeIn time.
I would actually like to know if my code is less effective in any way and if so, why?

1 Like

Interesting idea to use recursion. :grinning:

The problem with this approach is that Time.deltaTime isn’t clearly defined when called outside of the Update method. So I don’t think your fadeInTime ends up working as expected.