So I made this game on unity 5.5 and today I decided to update it to 5.6 finally. I use coroutines to darken the scene when loading another scene, so after updating Unity I realised that whenever a coroutine hits “yield” it just stops the coroutine altogether. What’s up with that? I see absolutely no errors whatsoever, it just dies out.
I checked the timeScale, the object neither of them have any issues. I mean… it works on 5.5 after all. So what could be the problem here?
IEnumerator SceneOpener(GameObject canvas)
{
GameObject loadImage = new GameObject("LoadImage");
loadImage.transform.parent = canvas.transform;
Image loadimg = loadImage.AddComponent<Image>();
Color32 loadCol = new Color32(0, 0, 0, 255);
loadimg.color = loadCol;
RectTransform loadRect = loadImage.GetComponent<RectTransform>();
loadRect.anchorMin = new Vector2(0, 0);
loadRect.anchorMax = new Vector2(1, 1);
loadRect.localScale = Vector3.one;
loadRect.offsetMin = new Vector2(loadRect.offsetMin.x, 0);
loadRect.offsetMax = new Vector2(loadRect.offsetMax.x, 0);
loadRect.offsetMin = new Vector2(loadRect.offsetMin.y, 0);
loadRect.offsetMax = new Vector2(loadRect.offsetMax.y, 0);
while (loadimg.color.a > 0f)
{
loadCol.a -= 51;
loadimg.color = loadCol;
yield return new WaitForEndOfFrame();
}
UnityEngine.Object.Destroy(loadImage);
}
here’s my coroutine and I call it on Start() like this: StartCoroutine(SceneOpener(canvas));
as I said this used to work without any problems on 5.5, I also thought that Unity wouldn’t let such a simple thing pass by, but I really have no idea about the issue.
As far as I’ve seen in the debug mode it just hits the yield and then the coroutine stops.
What else is going on in that scene? There is a good chance something else might be creating an issue.
Does Unity freeze(as in you can’t hit play to stop it) or the code just appears to stop running? If you put a debug in your while loop, does the debug keep printing? Does it print once or multiple times?
Also, for fades and such in the future, you may consider a tweening engine. They handle them really well.
Unless unity staff pop in this thread saying it’s a known issue then your problem will not go away without bug report. This can be a really slim small reproduction scene if you like. So long as the scene works in 5.5, and does not work in 5.6.
I see that you also use a Canvas in the script that’s giving you the problem. We’ve been testing (and posting our results here), and for us the cause seems to be the use of canvas and coroutines in the same script…
We don’t know what causes this yet, although it looks like a bug. We’ve rolled back to 5.5 for now in hopes that this get’s solved.
@aicam-spain canvas and coroutines being in the same script causing the problem? That is really strange. Nonetheless I have filed a bug report today, hopefully there will be a fix for this. Although in this case I cannot remove the canvas usage in my script, but I will try it elsewhere as you’ve pointed out.
It seems like WaitForEndOfFrame is the most obscure of all the yields. The docs (which seem to have a wrong code example) say it’s meant for taking a screen shot (which you could also do in, ummm, OnRenderImage?) I’m also wondering if Application.CaptureScreenshot doesn’t make it obsolete. So it’s possible 0 people in the world are using WaitForEndOfFrame for it’s intended purpose. It sort of makes sense for it to bug out with a canvas, since that might draw last, affecting when the frame really ends.
For code just fading an alpha, I think yield return null; would be the more common way.
@Owen-Reynolds well they should have said it more obviously I guess, but I assure you the problem is not ‘WaitForEndOfFrame’, I tried other yield returns, it just dies out whatever I use.
I’ve had the same issues with simple coroutines and even some function calls when moving up to 5.6. I tried to make a bug report but when a make a new project with a simplified version of the issues the problem disappears.
I even tried updating to 2017 to get the fix for issue #904415, since that certainly sounded like what was going on, but at least in my case, the problem persists. I’m currently looking into isolating the issue.