Weird issue with l_ElapsedTime value only incrementing properly one time
Hello,
I am making a splash screen scene for my game and am trying to alter the alpha value of an overlaying black image to give the effect of fading in and out the splash image. I have gotten it to fade in with no problems, however when I am trying to fade it back out my l_ElapsedTime is not incrementing by Time.deltatime, instead the elapsed time value bounces around to different amounts... usually (but not always) below .333. I tried incrementing by 0.5f instead of Time.deltatime, and ran into a similar problem (the value usually remaining at 0.5).
I have tried a number of things but have run into the same problem each time. I tried having a completely separate function that performs the fade out effect, using different values for elapsed time for fade in and fade out, not using the += operand (instead typing it entirely out), using StopCoroutine, and more. Not sure where to go with this now. Oddly I am also unable to get the splash screen sound to play as well.
Code inserted below, I appreciate any help you can offer.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class FadeLogo : MonoBehaviour {
// **Global Variables**
[SerializeField]
private float g_FadeTime = 2.5f; // The amount of time it takes to fade the image in or out.
[SerializeField]
private AudioSource g_SoundEffects; // The audio source for the splash screen sound, which will be played back on the Sound Effects mixer output.
[SerializeField]
private AudioClip g_SplashSound; // The audio clip which will be played.
[Header("UI")]
[SerializeField]
private CanvasGroup g_FadeCanvas; // The canvas with image to fade in and out.
private bool g_FadeIn = true;
private bool g_FadeOut = false;
// **Functions**
// Update is called once per frame
void Update () {
StartCoroutine (SplashLoop());
}
private IEnumerator SplashLoop()
{
yield return StartCoroutine (FadeScreen(g_FadeIn));
StopCoroutine("FadeScreen");
yield return StartCoroutine (DelayBeforeFadeOut());
yield return StartCoroutine (FadeScreen(g_FadeOut));
g_FadeCanvas.alpha = 1.0f; // This here to ensure the image stays faded out.
}
// This function will fade in or out the splash screen image by adjusting the FadeCanvas' alpha value.
private IEnumerator FadeScreen (bool FadeIn)
{
float l_ElapsedTime = 0.0f;
while (l_ElapsedTime < g_FadeTime)
{
if (FadeIn) // Fade in the splash logo
{
g_FadeCanvas.alpha = 1.0f - (l_ElapsedTime / g_FadeTime);
}
else // It's not set to fade in, so fade out the splash logo
{
g_FadeCanvas.alpha = (l_ElapsedTime / g_FadeTime);
}
l_ElapsedTime += Time.deltaTime;
Debug.Log(l_ElapsedTime + "Fade: " + FadeIn);
yield return null;
}
}
// This function delays the processing of the FadeScreen function to fade out splash screen, allows the user to see the splash screen unfaded and hear the splash screen sound.
private IEnumerator DelayBeforeFadeOut()
{
g_SoundEffects.clip = g_SplashSound;
Debug.Log("Sound ready to play: " + g_SoundEffects.clip.loadState);
g_SoundEffects.Play();
yield return new WaitForSeconds(g_SoundEffects.clip.length + 1.0f);
}
}
Answer by KampKounslr · Dec 02, 2015 at 07:19 PM
Figured it out. Had the splash loop being called in Update instead of Awake. Blargh.
Your answer
Follow this Question
Related Questions
Doubts with rotation, deltatime and Clamp 0 Answers
Best way to run a long algorithm in the background? 1 Answer
Strange Problems in long scripts 0 Answers
Yield return statement ends loop execution 0 Answers
"Can't add script behaviour AICharacterControl. The script needs to derive from MonoBehaviour!" ? 0 Answers