- Home /
Coroutine For Loop Will Not End
Hello everyone, I have been trying to accomplish a screen fade effect using a coroutine, but I can't actually get the For loop condition to ever be met.
In the code below, the script will get very close to "counter >= value" but will never get there. In this case .1, but if I change it to say .3 instead, then the script will get very close to .3 but never reach it.
If anyone has an idea or answer why this is happening, I would love to know.
IEnumerator FadeToClear (float time) //after load is done, wait then fade into game (screen -> clear),
{
//yield return new WaitForSeconds (time);
for (float counter = 1; counter >= 0.1f; counter -= fadeSpeed * Time.deltaTime)
{
Debug.Log ("counter is " + counter);
//Debug.Log ("reducing panel opacity " + panelLoadingScreen.alpha);
panelLoadingScreen.alpha = counter;
yield return null;
}
if (panelLoadingScreen.alpha <= 0.1f)
{
Debug.Log ("turning off loading screen");
cameraLoadingScreen.SetActive(false);
}
}
The code looks ok to me. Do you change the value of fadeSpeed in other parts of your code?
Have you tried changing fadeSpeed * Time.deltaTime to a constant just to test?
@ zee_ola05: Indeed I have, I used to have it just be "counter -= 0.1f", same problem. In fact, when watched closely, the value approaches the condition, gets close to it, then starts to increase, then decrease, then increase, etc...very strange.
@ zee_ola05: -not doing anything else with "fadeSpeed". -It could be something with update, it is called at the end of this script: (as I think about it, I used to have a line of code that disabled the script after the GO was not null so it only ran 1 time...I suppose that since that line is gone, since the coroutine is still running then Update is still Updating...
void Update ()
{
//Debug.Log ("main menu script is updating");
worldRoot = GameObject.FindGameObjectWithTag("WorldRoot");
if (worldRoot != null) //does not count additive levels as true loads
{
panelLoadingScreen = GameObject.Find ("ContainerLoadingScreen").GetComponent<UIWidget>();
cameraInGameUI.SetActive (true);
loadingScreenCamera.SetActive (false);
//camera$$anonymous$$ovement = GameObject.Find("0$$anonymous$$ainCamera").GetComponent<Camera$$anonymous$$ovement>();
//camera$$anonymous$$ovement.rightVirtualAnalogStick = GameObject.Find("SpriteVirtualAnalogStickRight").GetComponent<VirtualAnalogStick>();
player$$anonymous$$ovement = GameObject.Find("PlayerPrefab").GetComponent<Player$$anonymous$$ovement>();
player$$anonymous$$ovement.leftVirtualAnalogStick = GameObject.Find("SpriteVirtualAnalogStickLeft").GetComponent<VirtualAnalogStick>();
camera$$anonymous$$ovement = GameObject.Find("0$$anonymous$$ainCamera").GetComponent<Camera$$anonymous$$ovement>();
camera$$anonymous$$ovement.rightVirtualAnalogStick = GameObject.Find("SpriteVirtualAnalogStickRight").GetComponent<VirtualAnalogStick>();
if (useSavedDataToggle.loadSavedData == true)
{
//loadSavedGame.enabled = true; //turn the script back on,
//loadSavedGame.shouldLoadSavedData = true; //load the saved data,
}
//this.enabled = false;
StartCoroutine (FadeToClear(waitToFadeIn)); // new way of fading into game 3 8 14,
}
}
You are starting the coroutine every frame - you only want to do that once.
Answer by fafase · Mar 10, 2014 at 07:47 AM
Are you making sure your coroutine is called only once? I mean it could be that you are triggering it many times in the Update and they just pile and what you see (values up and down) is just the result of many different coroutines doing their jobs.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Trouble with Iterative Coroutine 1 Answer
Problem with getting a value from a enum 2 Answers
How to loop a video texture in C# 1 Answer