Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to do infinite loop without getting unity crash?

Discussion in 'Scripting' started by blackdragonstory, Sep 28, 2013.

  1. blackdragonstory

    blackdragonstory

    Joined:
    Nov 21, 2010
    Posts:
    99
    The title says it all.
    Whenever I do a infinite loop unity crashes and that's discouraging me a lot.

    What I wanted to do is count from 1 to 3 with a delay of 3 seconds between them.
    When it reaches 3 and plays 3 seconds delay it's suppose to switch back to 1 and redo everything.
    Basicly it's a set of 3 gui images that change to show different help tips.

    This is for waiting 5 seconds between every number.
    Code (csharp):
    1. for(time_count = 1;time_count<4;time_count++){
    2. yield WaitForSeconds(5);
    3. }
    I tried various ways of looping,I used while,for... and I managed to make it count from 1 to 3 and go back to 1 and count again but the numbers didnt count 1,2,3.Sometimes insted of 2, three would appear and then after 5seconds it would be 3 again.

    So help me please :)
     
  2. crazysurge

    crazysurge

    Joined:
    Sep 18, 2013
    Posts:
    17
    While(1==1) might crash but for the seond delay try # wait
     
  3. Durazell

    Durazell

    Joined:
    Sep 28, 2013
    Posts:
    9
    How about something like:

    InvokeRepeating(repeatingFunction, initalDelay, repeatTime);

    function repeatingFunction() {
    //change image here or if you can't change it outside gui change some variable to select next image
    }
     
  4. blackdragonstory

    blackdragonstory

    Joined:
    Nov 21, 2010
    Posts:
    99
    I am not quite sure how to apply this to my code.
    Does repeatTime have to be some number or can it be boolean as well?
    And also,where does InvokeRepeating has to be.Inside some function or can it stay outside of it?
     
  5. Toerktumlare

    Toerktumlare

    Joined:
    Sep 15, 2013
    Posts:
    74
    Could be so that when you are looping you are locking up the thread. Could be so that you need to start your loop in its own thread.
     
  6. blackdragonstory

    blackdragonstory

    Joined:
    Nov 21, 2010
    Posts:
    99
    tnx for help :)
    I was looking at some of the scripts people did and I got a better understanding of the looping functions and stuff...
    So I tried once more with while statment and it actually worked :D

    Here is the code if someone finds it might usefull :

    I just needed something that would allow me to end the loop,so with adding input to end it there isnt crash.
    yay :D
     
  7. Tom163

    Tom163

    Joined:
    Nov 30, 2007
    Posts:
    1,273
    Why roll your own?

    Code (csharp):
    1. var timer = 3.0;
    2. var countdown=3.0;
    3.  
    4. function Start() {
    5.    countdown = timer;
    6. }
    7.  
    8. function Update() {
    9.    countdown -= Time.deltaTime();
    10.    if (countdown <= 0) {
    11.       // do whatever you want, switch images or whatever
    12.       countdown = timer;
    13.    }
    14. }
     
  8. blackdragonstory

    blackdragonstory

    Joined:
    Nov 21, 2010
    Posts:
    99
    tnx Tom :)
    Latter on when I played the code with while I got some numbers changing really quick for some reason.
    So I asked my friend who is also a coder and he told me to try using two timers.
    Pretty much the sam way you did it and I got it perfectly :D