- Home /
Sprite Fade In/Out Not Working
I'm new to Unity and C#. I'm trying to make a script where a 2D sprite will fade in and remain onscreen for a certain number of seconds, then fade out and stay faded out for a certain number of seconds, and then fade back in and repeat the cycle. I found some info about fades in the forums and tried to modify it to do what I want. But when I play my game, the sprite fades in as planned, then stays there for the right amount of time, and then disappears without fading. It then stays out for the right amount of time and then instead of fading back in, it just appears without fading. The cycle then continues onward like that; the fades stop working after the first initial fade in. Here's what I have for my script:
using UnityEngine;
using System.Collections;
public class FadeInOut : MonoBehaviour {
public float minimum = 0f;
public float maximum = 1f;
public float durationIn = 1.5f;
public float durationOut = 3.0f;
private float startTime;
private int fadedIn;
public SpriteRenderer sprite;
void Start() {
sprite.color = new Color (1f, 1f, 1f, 0f);
fadedIn = 0;
}
void Update() {
if (fadedIn == 0) {
float t = (Time.time - startTime) / durationIn;
sprite.color = new Color (1f, 1f, 1f, Mathf.SmoothStep (minimum, maximum, t));
StartCoroutine("Wait1");
}
if (fadedIn == 1) {
float t = (Time.time - startTime) / durationOut;
sprite.color = new Color (1f, 1f, 1f, Mathf.SmoothStep (maximum, minimum, t));
StartCoroutine("Wait2");
}
}
IEnumerator Wait1() {
yield return new WaitForSeconds (5);
fadedIn = 1;
}
IEnumerator Wait2() {
yield return new WaitForSeconds (5);
fadedIn = 0;
}
}
Any help with this would be greatly appreciated!
Answer by OncaLupe · Dec 04, 2015 at 03:32 AM
The problem is you're not setting the startTime variable, so each time after the first the fade calculation instantly hits the end. A quick fix would be to set startTime = 0f
in the coroutines just after the yields.
However, the way the script is set up, the calculation and color setting is being done every frame, even when there is no actual change. Personally I'd use a single coroutine to handle everything like this:
using UnityEngine;
using System.Collections;
public class FadeInOut : MonoBehaviour
{
public SpriteRenderer sprite;
public Color spriteColor = Color.white;
public float fadeInTime = 1.5f;
public float fadeOutTime = 3f;
public float delayToFadeOut = 5f;
public float delayToFadeIn = 5f;
void Start()
{
StartCoroutine("FadeCycle");
}
IEnumerator FadeCycle()
{
float fade = 0f;
float startTime;
while(true)
{
startTime = Time.time;
while(fade < 1f)
{
fade = Mathf.Lerp(0f, 1f, (Time.time - startTime) / fadeInTime);
spriteColor.a = fade;
sprite.color = spriteColor;
yield return null;
}
//Make sure it's set to exactly 1f
fade = 1f;
spriteColor.a = fade;
sprite.color = spriteColor;
yield return new WaitForSeconds(delayToFadeOut);
startTime = Time.time;
while(fade > 0f)
{
fade = Mathf.Lerp(1f, 0f, (Time.time - startTime) / fadeOutTime);
spriteColor.a = fade;
sprite.color = spriteColor;
yield return null;
}
fade = 0f;
spriteColor.a = fade;
sprite.color = spriteColor;
yield return new WaitForSeconds(delayToFadeIn);
}
}
}