Unity Community


Results 1 to 15 of 15

  1. Posts
    116

    variable inside for loop doesn't stop counting

    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:  
    1. var timer = 0;
    2.  
    3. function Update ()
    4. {
    5. for( i = 1; i <= 10; i++)
    6.     {
    7.         timer ++;;
    8.         print("the timer is: " + timer);
    9.         print("the i value is: " + i);
    10.     }
    11. }

    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.


  2. Location
    Argentina
    Posts
    83
    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.


  3. Posts
    116
    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!


  4. Location
    Argentina
    Posts
    83
    Taking it out of the update function did fix it
    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.


  5. Posts
    116
    Wouldn't having a never ending for loop eventually crash the game though? Or anything that runs in an endless loop?


  6. Location
    Argentina
    Posts
    83
    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.


  7. Posts
    116
    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.


  8. Location
    Argentina
    Posts
    83
    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:  
    1. while(true)
    2. {
    3. }

    It will "crash" unity, in the way, that this infinite loop will never return, and the other Unity excution will never happen.

  9. Administrator



    Location
    London, UK
    Posts
    3,305
    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.


  10. Posts
    116
    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.


  11. Posts
    68
    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.


  12. Location
    Vancouver, Canada
    Posts
    156
    Yield statements only work inside a coroutine. I believe it is ignored otherwise.


  13. Posts
    68
    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.


  14. Location
    Vancouver, Canada
    Posts
    156
    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.


  15. Posts
    68
    Quote Originally Posted by BluntWeapon View Post
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •