- Home /
Is it possible to have a yield manager?
I am not sure why this wont work but, say if I dont want to have my functions all IEnumerators, could I instead have a function what manages yields? It doesnt work when I call it for some reason.
void Awake()
{
StartCoroutine("fakeYield", 0);
}
and this is the function :
IEnumerator fakeYield(float duration)
{
Debug.Log("Waiting for "+duration);
yield return new WaitForSeconds(duration);
}
and I am calling it like this :
void 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++)
{
Vector3 a = loadedChunks[i].transform.position;
if (a.x == centerChunk.pos.x - 96)
{
fakeYield(0.1f);
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;
It debugs it once, during the awake function for some reason and whenever the loop calls it, it doesn't call the function for some reason...
Answer by Bunny83 · Jun 13, 2013 at 01:37 PM
Your fakeYield function is a generator function. It doesn't execute any code inside your function, but it returns and object which can be used to "iterate through your code". This object need to be passed to StartCoroutine and Unity's coroutine scheduler will handle the iteration for you.
When you just call:
fakeYield(0.1f);
It will create an iterator object which you just throw away, so it does nothing. Besides that your fakeYield has no use since there's nothing after your
yield return new WaitForSeconds(duration);
The reArrangeChunkOrderToMatchSystem function can't be "paused" since it's not a coroutine.
What do you actually want to do?
I want to add a delay, look at this :
http://answers.unity3d.com/questions/473703/wierd-issue-with-coroutines.html#answer-473803
$$anonymous$$y other question which is what im trying to do to get around the issue im having, i just want to add a 0.1 second delay inbetween the for loop but i am really struggling with this stupidly simple code.
Your other version is by far more correct than this one. You can't add a delay in a normal function. It has to be a coroutine.
How and where do you call your reArrangeChunkOrderTo$$anonymous$$atchSystem coroutine in your other question?
I was calling the startcoroutine in the start function and trying to call the function normally in the code, i guess i cant really just call the ienumerator function normally can i?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Waiting between pingpong loops 2 Answers
What is wrong with this use of WaitForSeconds? 1 Answer
C# yield waitforseconds 3 Answers