- Home /
Wierd issue with Coroutines?
I am finding it very annoying to just yield in code. I have this :
IEnumerator reArrangeChunkOrderToMatchSystem(int dir)
{
GameObject[] newData;
if (dir != 6)
{
switch (dir)
{
case 0:
newData = new GameObject[loadedChunkRadius * worldChunkDefaultLoadedHeight];
for (int i = 0; i < loadedChunkRadius * worldChunkDefaultLoadedHeight; i++)
{
newData[i] = ChunkInstance;
}
int temp = 0;
for (int i = 0; i < loadedChunkRadius * worldChunkDefaultLoadedHeight * loadedChunkRadius; i++)
{
yield return new WaitForSeconds(0.1f);
Vector3 a = loadedChunks[i].transform.position;
if (a.x == centerChunk.pos.x - 96)
{
loadedChunks[i].transform.position += new Vector3(16 * (loadedChunkRadius - 1), 0, 0);
Vector3 vec = new Vector3(loadedChunks[i].GetComponent<ChunkMain>().chunk.pos.x, loadedChunks[i].GetComponent<ChunkMain>().chunk.pos.y, loadedChunks[i].GetComponent<ChunkMain>().chunk.pos.z);
reloadChunkDataOrGenerateNew(vec, loadedChunks[i]);
temp++;
}
}
break;
The issue is that it wont wait for 0.1 seconds, it will just literally stop the whole function from working. I dont know whats going on and there are no errors but everything after the yield doesnt work any more. Am i doing something obvious that is wrong?
I also started the coroutine in the start function so it should work.
void Start ()
{
StartCoroutine("reArrangeChunkOrderToMatchSystem",6);
}
All i want to really do is add a delay to the part where the yield is currently placed so that it slowly streams the data rather than doing everything in a chunk.
Thanks,
How large is ChunkRadius squared times WorldHeight? $$anonymous$$aybe it is running, just taking forever. Try some Debugs.
Is centerChunk moving in that 10th of a second? The odds of a.pos.x being exactly equal to center.pos.x-96 are virtually nil, if so.
I instantiate my system on a grid, it is dynamically offset in sets of 16 so it will always be equal to an integer, nothing moves them around either, it works perfectly fine without the yield but once i use the yield it doesnt work, no errors, it just doesnt work.
Like owen said, how many iterations are you doing in your for loop? For example if you have 16 16 128 (a bit $$anonymous$$ecraft related :D) you will have 32k iterations. Even when you yield 0.00000001s the yield will at least take one frame, so just assume we have a constant framerate of 60 fps that would mean your for loop takes 546 seconds or over 9 $$anonymous$$utes. So can you please give us your bounds.
it is 11x3x11 ( the loaded chunks in the world at once ) , basically 363 loops. the reason i use variables is so that i can make the system dynamic, also it is the same variable X itself because the x and z loaded range is always the same.
Of course, we never said you shouldn't use variable, but it's hard to debug the script if you don't know whether the value in those variables are in the range 2-5 or 200k - 5m
With your delay of 0.1 second it takes about one $$anonymous$$ute to complete the loop.
Answer by aldonaletto · Jun 13, 2013 at 01:12 AM
The yield is correct - it should cause a 0.1 delay each loop iteration, and without hanging Unity. Maybe your problem has another cause. Is this for correct?
for (int i = 0; i < loadedChunkRadius * worldChunkDefaultLoadedHeight * loadedChunkRadius; i++){
loadedChunkRadius is being multiplied by itself and by worldChunkDefaultLoadedHeight, and if there's no enough loadedChunks elements you will get a runtime error (Out of Range), aborting the script. Should loadedChunkRadius be multiplied by itself?
Yes it should be multiplied by itself, i use variables ins$$anonymous$$d of numbers because it allows the system to be changeable. It works fine without the yield, its just as soon as i put that yield in there, it will just not execute the code, it wont give any errors either, it just, doesnt work when i add the yield. which is really really wierd!
Answer by AntiLunchBox · Jun 13, 2013 at 05:26 PM
im a little confused how your code is getting there in the first place. You are calling that function with 6 as the dir parameter...However, in the beginning it checks if dir != 6 ???
I did that because it was calling it in the start function and giving an error because it was initialized , i dont get why that matters what parameters i set, i tried 0 and it still doesnt work. I really dont get why it wont work.
Your answer
Follow this Question
Related Questions
Getting an error when trying to yield 2 Answers
yield works, but yield WaitForSeconds does not? 1 Answer
Alternative for semaphores in Unity? 2 Answers
Playing footsteps with interval 1 Answer
Load Level when dead for 1 second 2 Answers