Search Unity

Is while(condition) { yield return null;} safe?

Discussion in 'Scripting' started by egoquat, Dec 16, 2014.

  1. egoquat

    egoquat

    Joined:
    Jul 4, 2012
    Posts:
    93
    Hello. I wonder which is better safely on commercial projects "yield return condition;" with
    "while(condition) { yield return null;}". Thanks.
     
  2. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    Code (CSharp):
    1. //This loop will go infinitely.
    2. while(true){
    3.     //Stuff here
    4. }
    5. //This loop can only go 10 times
    6. int x = 0;
    7. while(x < 10){
    8.     x += 1;
    9. }
    while loops are safe as long as there is a very rare or never chance they will run infinitely
     
  3. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    while loops are perfectly safe withing co-routines as long as you yield. Even while(true) loops.
     
  4. egoquat

    egoquat

    Joined:
    Jul 4, 2012
    Posts:
    93
    Thank you for all replies.