Unity Community |

I'm watching Walker Boys Studio videos on the for loops and I'm using the script he's using except mine won't stop looping.
Code:
Unity will acknowledge that i has reached 10 and i doesn't go any further but timer will continue to increase infinately. I don't understand why the for loop doesn't break once i is 10. Everything I've seen so far says this should work. I even looked on W3Schools and did the same for loop and it worked just fine.
It breaks when it reachs 10, but it starts again and again because you put it inside the Update method.
Update method is called once per frame.
Taking it out of the update function did fix it but I don't know why. In the video, he is clearly putting his for loop in the update function. I'll just make a note on my script about it for the future. Thanks for the help!![]()
The thing is that there is nothing to fix, it just depends on what you want to do. If you want the loop being processed each frame, it's ok, if not you should put it in anothor function and call it only when you need.Taking it out of the update function did fix it
Wouldn't having a never ending for loop eventually crash the game though? Or anything that runs in an endless loop?
The game itself is a infinite loop. In fact, if it wasn't, it will stop it execution eventually.
What you have there is a finite loop inside a funtion called each frame in the main infinite loop of the program.
I think I understand what you're saying but I've always heard programmers say if you create an infinate loop, your computer with set itself on fire. Not quite that dramatic of course but program crashing is always the end result.
well, the the thing is:
the operative system is an infinite loop program, that is running constantly
OS manange every program that are running on it (that's why you can use many programs at the same time)
each program being or not in an infinite loop, get and retrieve informations from/to the OS
if a program enters (for some) reason in an unamaged infinite loop, it stops comunicating with the OS, it the program basicaly crashes.
If you want to test it just put in Start (or in the Update method)
Code:
while(true) { }
It will "crash" unity, in the way, that this infinite loop will never return, and the other Unity excution will never happen.
Unity reads all of the relevant code in your scene and renders a frame. Then Unity goes back to the top of the code and does it all over again. This is the "game loop". The function Update () {} runs once per loop or once per frame. All of the code in function Update is called once per frame. This is why it's good for things like checking for player input or moving an objects transform. If you put code in update, it will get called every game loop before it renders the frame. If you want to execute code every frame, function Update is a good place to put it.
Find Answers!
The Google Custom Search Engine for Unity3D: http://tinyurl.com/Unity3dSearchEngine
-
Learning Resources for Unity3D:
On the Unity3D forum | Unity3D Resources | Getting Started with Coding for Unity
Huh, yep I definately crashed Unity. If I'm understanding this correctly, say the Unity program itself is one big while loop. While Unity is running, stuff happens. You put in your functions and such for your game but everything eventually breaks and goes back to the main Unity while loop to be executed again. The while(true) loop will never ever break and thus the program will never get called back to the main Unity while loop so the program is stuck in this tiny section of code.
Now I'm curious why I can call for a function with a coroutine from the Update loop, and the function gets executed like specified in the coroutine. The yield statements work like I expect. And when I try the same principle with another function in the Update function, the coroutine won't work and the function gets called every frame and ignores the yield statements. I don't understand why this happens while I'm basically doing the same as before.
Yield statements only work inside a coroutine. I believe it is ignored otherwise.
I'm aware of that
That's why I made a function with a coroutine, and call that function from update. It works like I expect, the function gets called, and the coroutine gets executed for as long as I specified and when I expect it.
When I try to do something similar I'm getting a totally different result, the function also gets called from Update, but ignores the yield statements.
I don't mean to jack topics though, I'm just reading through things and saw something related to my problems. I'm trying to get a sense of something that doesn't make sense to me.
Could you be a little more specific about what it is that you're trying to do? I mean Coroutines are coroutines...I don't think I've ever seen them behave differently across different coroutine functions.
I wrote this little script to help someone in this topic http://forum.unity3d.com/threads/134...ountdown-timer
I've attached the script inside the topic so this topic won't get irrelevant posts (and I apologize if it already has i saw some correlation between these 2 topics) so if you could help me please check the topic.