- Home /
Question by
sauravdhanola · Oct 10, 2019 at 01:36 AM ·
coloralphadocumentationsprite rendererfading
Unity documentation says to use StartCoroutine() method for visible fading of renderer alpha but the following method works fine without StartCoroutine() with visible intermediate values. Is this way also right ?
using UnityEngine;
public class fade4 : MonoBehaviour {
private SpriteRenderer sr;
void Start ()
{
sr = GetComponent<SpriteRenderer>();
}
void Update ()
{
Fade();
}
void Fade()
{
Color c = sr.color;
c.a = c.a - 0.5f * Time.deltaTime;
sr.color = c;
if (c.a <= 0.1f)
this.enabled = false;
}
}
Comment
It will work yes, however, why create a class for a short existence when if you used a coroutine, you could also use it to fade in as well as out? Also, I would use Time.deltaTime so it reduces at a set speed no matter the framerate.
Answer by joshuapeacock · Oct 10, 2019 at 05:20 AM
Yes, that is achieving the same thing, Coroutines can give you more control over when Fade is called. You could set a tick rate for your function or wait until the end of the frame for example.