- Home /
spliting my code in a coroutine with yield return null?
Hi, I've got an idea to split some of my heavy coroutines to execute them in multiple frames. but I got some weird result, in some cases it's working and in some it's not. here is a piece of code that doesn't work:
IEnumerator LoadSettings()
{
loading = true;
QualitySettings.masterTextureLimit = 4 - GameSettings.TextureQuality;
QualitySettings.lodBias = GameSettings.LODBias;
QualitySettings.anisotropicFiltering = GameSettings.Anisotropic ? AnisotropicFiltering.Enable : AnisotropicFiltering.Disable;
QualitySettings.antiAliasing = GameSettings.AntiAliasing;
//if user wants some dyamic lighting we must enable useLightProbe option in every meshRenderer and SkinnedMeshRenderer
if (!GameSettings.DynamicLighting)
{//user doesn't want dynamic lighting
var meshRenderers = FindObjectsOfType<MeshRenderer>();
var skinnedMeshRenderers = FindObjectsOfType<SkinnedMeshRenderer>();
foreach (var meshRenderer in meshRenderers)
{
meshRenderer.useLightProbes = false;
yield return null;
}
foreach (var skinnedMeshRenderer in skinnedMeshRenderers)
{
skinnedMeshRenderer.useLightProbes = false;
yield return null;
}
}
else
{//user wants dynamic lighting
var meshRenderers = FindObjectsOfType<MeshRenderer>();
var skinnedMeshRenderers = FindObjectsOfType<SkinnedMeshRenderer>();
foreach (var meshRenderer in meshRenderers)
{
meshRenderer.useLightProbes = true;
yield return null;
}
foreach (var skinnedMeshRenderer in skinnedMeshRenderers)
{
skinnedMeshRenderer.useLightProbes = true;
yield return null;
}
}
yield return null;
//user can customize game resulotion to decrease or increase the fill rate of the game
Screen.SetResolution(GameSettings.ResulotionWidth, GameSettings.ResulotionHeight, true);
//locking the aspect
var _cameraGameObjects = FindObjectsOfType<Camera>();
foreach (var camera in _cameraGameObjects)
camera.GetComponent<Camera>().aspect = 1920f / 1080f;
loading = false;
}
I used Visual Studio and inserted some breakpoints and I noticed my code doesn't reach to this line:
skinnedMeshRenderer.useLightProbes = true;
I am pretty sure that I have some SkinnedMeshRenderers in my scene.
So whats the problem with this coroutine?
Answer by Denscrivent · Jul 28, 2016 at 04:28 AM
I think you should use just 1 yield return null at the very bottom of the coroutine function
That makes no sense. Then you don't need a coroutine at all. The point of a coroutine is to interrupt the execution in between. A yield at the end is always completely useless. As it interrupts and when it comes back it does nothing but ending the coroutine.
The problem is every coroutine works well with this pattern except this one. :/
Answer by Bunny83 · Jul 28, 2016 at 09:24 AM
Well, i suggest you set your breakpoint at the start of your coroutine and step through it and inspecting some values. So you can check what value "GameSettings.DynamicLighting" has and if FindObjectsOfType actually returned anything. Just hover over a variable while the process is paused and you're inside the coroutine.
In VS you have to press F10 to "step over" the next instruction or F11 to "step into" which only makes sense for lines that call other methods.
Are you sure you attached VS as debugger? If you don't breakpoints are pretty useless ^^. I'm not sure if you can actually place breakpoints in coroutines but it's worth a try.
It's very very very weird , take a look at this picture, the meshRenderers array is null, but it will goes through the loop!!!!!!!!!!!!!!!!!!!!!!!!!!!!! I think I must mention that I use unity 4.7.2 (maybe it has some kind of bug or something):
Edit: This coroutine will start at beginning of the game before everything else appear.
Your answer
Follow this Question
Related Questions
Coroutine sequence not running properly 1 Answer
Coroutines not passing yield 1 Answer
Getting an error when trying to yield 2 Answers