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. Dismiss Notice

Please Don't Laugh

Discussion in 'Scripting' started by BillyBobBeavis, May 30, 2021.

  1. BillyBobBeavis

    BillyBobBeavis

    Joined:
    Oct 13, 2018
    Posts:
    97
    Is there some way to shorten this?
    Code (CSharp):
    1. IEnumerator ChoosePath()
    2.     {
    3.         yield return null;
    4.         ChoosePathMethod();
    5.         yield return null;
    6.         ChoosePathMethod();
    7.         yield return null;
    8.         ChoosePathMethod();
    9.         yield return null;
    10.         ChoosePathMethod();
    11.         yield return null;
    12.         ChoosePathMethod();
    13.         yield return null;
    14.         ChoosePathMethod();
    15.         yield return null;
    16.         ChoosePathMethod();
    17.         yield return null;
    18.         ChoosePathMethod();
    19.         yield return null;
    20.         ChoosePathMethod();
    21.         yield return null;
    22.         ChoosePathMethod();
    23.         yield return null;
    24.         ChoosePathMethod();
    25.         yield return null;
    26.         ChoosePathMethod();
    27.         yield return null;
    28.         ChoosePathMethod();
    29.         yield return null;
    30.         ChoosePathMethod();
    31.         yield return null;
    32.         ChoosePathMethod();
    33.         yield return null;
    34.         ChoosePathMethod();
    35.         yield return null;
    36.         ChoosePathMethod();
    37.         yield return null;
    38.         ChoosePathMethod();
    39.         yield return null;
    40.         ChoosePathMethod();
    41.         yield return null;
    42.         ChoosePathMethod();
    43.         yield return null;
    44.         ChoosePathMethod();
    45.         yield return null;
    46.         ChoosePathMethod();
    47.         yield return null;
    48.         ChoosePathMethod();
    49.         yield return null;
    50.         ChoosePathMethod();
    51.         yield return null;
    52.         ChoosePathMethod();
    53.         yield return null;
    54.         ChoosePathMethod();
    55.         yield return null;
    56.         ChoosePathMethod();
    57.         yield return null;
    58.         ChoosePathMethod();
    59.         yield return null;
    60.         ChoosePathMethod();
    61.         yield return null;
    62.         ChoosePathMethod();
    63.         yield return null;
    64.         ChoosePathMethod();
    65.         yield return null;
    66.         ChoosePathMethod();
    67.         yield return null;
    68.         ChoosePathMethod();
    69.         yield return null;
    70.         ChoosePathMethod();
    71.         yield return null;
    72.         ChoosePathMethod();
    73.         yield return null;
    74. }
     
  2. Omniglitch

    Omniglitch

    Joined:
    May 29, 2017
    Posts:
    37
    Typically, this repetition is done with a loop, such as a for loop, but I've never tried using a loop inside a coroutine before so I can't say for sure it will work.

    Code (CSharp):
    1. IEnumerator ChoosePath()
    2. {
    3.     for ( int i = 0; i < 35; i++ ) {
    4.             yield return null;
    5.             ChoosePathMethod();
    6.     }
    7.     yield return null;
    8. }
     
    stain2319 likes this.
  3. nsxdavid

    nsxdavid

    Joined:
    Apr 6, 2009
    Posts:
    476
    You absolutely can use a loop inside of a coroutine. In your case, I'd swap lines 4 and 5. And delete line 7.

    Like so:


    Code (CSharp):
    1. Enumerator ChoosePath()
    2. {
    3.     for ( int i = 0; i < 35; i++ ) {
    4.            ChoosePathMethod();
    5.             yield return null;
    6.     }
    7. }
    8.  
     
    Lurking-Ninja and stain2319 like this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Beware this simply yields until the next frame, generally speaking.

    As in, why not throw
    ChoosePathMethod()
    in an Update() loop and call it a day?

    If you truly only want it running 35 times, then sure, I guess that's a way to do it, but a counter might be a lot more straightforward.

    Code (csharp):
    1. private int countRemaining;
    2. void Start()
    3. {
    4.   countRemaining = 35;
    5. }
    6. void Update()
    7. {
    8.   if (countRemaining > 0)
    9.   {
    10.     ChoosePathMethod();
    11.     countRemaining --;
    12.   }
    13. }
     
  5. BillyBobBeavis

    BillyBobBeavis

    Joined:
    Oct 13, 2018
    Posts:
    97
    Fantastic! Thanks so much!
     
  6. BillyBobBeavis

    BillyBobBeavis

    Joined:
    Oct 13, 2018
    Posts:
    97
    Thank you!
     
  7. BillyBobBeavis

    BillyBobBeavis

    Joined:
    Oct 13, 2018
    Posts:
    97
    Right. Thank you.
     
    Last edited: May 31, 2021