Hey guys! I’m trying to do the following:
If the Coroutine “SwitchFadeCycle” is called, the whitescreen texture should fade in (over 0.5 seconds) and fade out (over 0.5 seconds) right after that. It fades in properly, but doesn’t fade out. The color value doesn’t change. Each script (the one for fading in and the one for fading out) seems to be correct (if I change the fade value to 1 manually it does fade out instead of fading in).
using UnityEngine;
using System.Collections;
public class WhiteScreenFadeInAndOut : MonoBehaviour {
public SpriteRenderer WhitescreenSR;
public Color spriteColor = Color.clear;
public float fadeInTime = 0.5f;
public float fadeOutTime = 0.5f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public IEnumerator SwitchFadeCycle()
{
float fade = 0f;
float startTime;
WhitescreenSR.enabled = true;
while(true)
{
startTime = Time.time;
while(fade < 1f)
{
fade = Mathf.Lerp(0f, 1f, (Time.time - startTime) / fadeInTime);
spriteColor.a = fade;
WhitescreenSR.color = spriteColor;
yield return null;
}
//Make sure it's set to exactly 1f
fade = 1f;
spriteColor.a = fade;
WhitescreenSR.color = spriteColor;
yield return null;
startTime = Time.time;
while(fade > 0f)
{
fade = Mathf.Lerp(1f, 0f, (Time.time - startTime) / fadeOutTime);
spriteColor.a = fade;
WhitescreenSR.color = spriteColor;
yield break;
}
fade = 0f;
spriteColor.a = fade;
WhitescreenSR.color = spriteColor;
yield return null;
}
}
}
My guess is your fade never gets set to 1f. I would put some prints/debug.log statements in your first while loop and see what values you get to see if it ever is >= 1f.
Though the lack of proper spacing and tabbing is making this hard to read, so it’s just a quick guess. Also make sure you’re not accidentally calling your fade more than once.
You don’t need the outer while(true) loop. And because of poor code formatting its very difficult to find the issue. However here is a simple color fade coroutine:
IEnumerator Fade_CR(Color color, float fadeInTime, float fadeOutTime)
{
float t = 0f;
while (t<fadeInTime)
{
color.a = t/fadeInTime;
t += Time.deltaTime;
yield return null;
}
yield return new WaitForSeconds(1); // wait for 1 second before fading out
t = 0;
while (t<fadeOutTime)
{
color.a = 1 - (t/fadeOutTime);
t += Time.deltaTime;
yield return null;
}
}
I’m sorry, I didn’t use the proper command to inser the code. I edited my opening post.
/ I removed the outer while function and added some prints. Fade actually reaches 1 at the point it is supposed to, but from then on the script doesn’t seem to continue.
@Brathnann : I have no idea. I’ve been into scripting for a few days now, I found that code on the internet and tried to modify it a bit. A very similar code works well on another fading process, but I seem to be good at breaking stuff.
using UnityEngine;
using System.Collections;
public class WhiteScreenFadeInAndOut : MonoBehaviour {
public SpriteRenderer WhitescreenSR;
public Color spriteColor = Color.clear;
public float fadeInTime = 1f;
public float fadeOutTime = 1f;
public float delay = 1f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public IEnumerator SwitchFadeCycle () {
float t = 0f;
while (t < fadeInTime) {
spriteColor.a = t / fadeInTime;
t += Time.deltaTime;
print (t);
yield return null;
}
yield return new WaitForSeconds (delay);
t = 0;
while (t < fadeOutTime) {
spriteColor.a = 1 - (t / fadeOutTime);
t += Time.deltaTime;
print (t);
yield return null;
}
}
}
However, as soon as the process starts it modifies the alpha a tiny bit. At the first print t is around 0.016 and at the second print around 0.03.
I’m really sorry to bother you, and I gotta admit, I don’t fully understand your code, and let’s not even talk about the original one. If you could tell me what’s wrong and at which point, that’ll surely help me with my scripting knowledge!
Time.deltaTIme is a small value, so it’s going to increment slowly. If you want it faster, you’ll need to multiply it by a speed value and then add it to t. This should get it there, otherwise, it will get there eventually at it’s current speed.
I would also suggest considering a tweening engine to make things easier when dealing with this sort of thing.
Both LeanTween and DoTween are free on the asset store and can handle fades very well. I use LeanTween myself and it’s used throughout many of our projects to handle fades and doing animations on UI. Works great.
Oh, and I asked about yield break because that I believe is breaking you out of your second while loop and thus not doing your second fade. But I’ve not used a yield break before, so it was just a thought.
I dont really get what you saying here. Are you talking about the value of alpha in first and second frame (as you are printing the value)?
If that is the case. Can you tell me if alpha goes from 1 to 0 and then from 0 to 1 in given time?