- Home /
Coroutine and terrain
Right now I am calling a coroutine like this.
Update
{
if (Gui.Button(new Rect(15,15,150,150),"test coroutine"){
StartCoroutine(TestC(200f));
}
IEnumerator TestC(float P){
float A = 0;
while (A<P)
Debug.Log(A);
A++;
}
yield return null;
}
My understanding of this is that TestC will take in 200 as a float (called P) and then do the while until A equals P - 1 (since it's < and not <=), at which point it returns nothing due to it being null. The yield is there since it tells the coroutine to yield to nothing and thus stop doing anything (I assume there are other names to use other then yield but that's the only one I know). I'm saying this to make sure I understand how coroutines work since I am new with them.
I do have one question though, aren't coroutines supposed to do code while your program runs so that you program doesn't jam? EX: A loading screen. If so, why does my program still freeze when running a coroutine? I'm doing the same thing as above, only I'm interating 2'000 entries and it still freezes instead of silently running like I expected it to.
Second question, I will post this in a seperate thread if no one wants to answer it here. I am going through my terraindata point by point to return the splats that are located there. My terrain is 2'000 units by 2'000 units. After reaching 512 sideways, I get error. . .
Texture rectangle is out of bounds (1999 + 1 > 512) UnityEngine.TerrainData:GetAlphamaps(Int32, Int32, Int32, Int32)
I actually had it working until I deleted my terrain and recreated it (my terrain seemed have unsynced from my scene, was the solution I found). I recreated the terrain and it's the only terrain in the scene, I also debug.log(terrain.activeterrain.terraindata.name) and it turns out to be the right terrain so I am curious as to what is happening. It looks like it trying to read outside of it's bounds (set to 512, even though my map is 2'000 and I debug.logged that too). Curious if anyone had this issue and know what setting it's reading for 512.
Answer by testure · Jul 24, 2011 at 04:25 AM
Formatted your code for you.
coroutines are not multithreaded. Unity is not multithreaded (yet). All processes happen on a single thread, which means if you have a while loop that runs infinitely and does not yield to the next instruction, it will hang the process.
To solve your problem, put your yield inside the while loop, not outside. This will ensure that the loop yields to the next instruction and doesn't hang.
Regarding your terrain question, I'm not sure- it might be better suited for its own question, since I came here because I know a lot about coroutines, but I haven't really used unity's terrains. others might do the same.
Thanks mate, yield was what I was looking for. What's the purpose of a coroutine then?
Your answer
![](https://koobas.hobune.stream/wayback/20220612154044im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
A node in a childnode? 1 Answer
IEnumerable called within a EventHandler. Can this be done? 1 Answer
Updating a TextMesh buggy? 1 Answer
Terrain texture painting 0 Answers