- Home /
Question on Possible FOR Loop Condition Conflict
Hello everyone, I've got a question about how to deal with a potential problem when using a FOR loop.
The script below is what I'm using to fade out a loading screen and transition to the in game UI. The script works, but there is a potential problem between the two values of the FOR loop and IF statement if deltaTime had a large value (say the game temporarily froze or had performance issues and the value spiked as a result of it), the IF statement would never be executed.
What is the more intelligent way of dealing with this? Perhaps making the IF statment value something like: 0.5f + (value * time.deltaTime)?
Any advice is appreciated.
public IEnumerator FadeToGame (float time) //after load is done, wait then fade into game (loading screen -> clear),
{
yield return new WaitForSeconds (time);
for (float counter = 1; counter >= 0.05f; counter -= fadeInFromLoadingScreenSpeed * Time.deltaTime)
{
Debug.Log ("the value of speed * delta time is " + (fadeInFromLoadingScreenSpeed * Time.deltaTime));
//Debug.Log ("counter is " + counter);
panelLoadingScreen.alpha = counter;
yield return null;
}
if (panelLoadingScreen.alpha <= 0.065f) //hopefully within the margin of error of deltatime,
{
//Debug.Log ("turning off loading screen");
panelLoadingScreen.alpha = 0;
cameraLoadingScreen.SetActive(false); //make sure this is off since it is only needed at beginning of games,
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
For Loop is Only doing it's Job Once 2 Answers
Using i in if statement 3 Answers
How do I Open/Close my option menu with one key C# 1 Answer
How to shorten multiple if statements ? 2 Answers