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

How do I make this coroutine run every 3 seconds?

Discussion in 'Scripting' started by JustaDuck97, Mar 26, 2020.

  1. JustaDuck97

    JustaDuck97

    Joined:
    Jul 31, 2019
    Posts:
    45
    I need this gameObject to instantiate another GameObject every three seconds without stopping. I'm just learning coroutines and need help understanding the different yield returns and I couldn't find a section in a Unity manual about yield returns.

    Here is what I need to run:

    Code (CSharp):
    1. IEnumerator DropAttack()
    2.     {
    3.         yield return new WaitForSeconds(3);
    4.          GameObject TrapAttack = Instantiate(SpikerPrefab, Dropper.position, Dropper.rotation);
    5.      
    6.     }
     
    shmemadt likes this.
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,742
    That coroutine is correct for waiting 3 seconds and spawning once. You can make a small tweak to make it do this in a loop forever:
    Code (csharp):
    1.  
    2. IEnumerator DropAttack()
    3.     {
    4.         while (true) {
    5.              yield return new WaitForSeconds(3);
    6.              GameObject TrapAttack = Instantiate(SpikerPrefab, Dropper.position, Dropper.rotation);
    7.          }
    8.     }
    (Note: be a little bit careful when using a while loop - if you accidentally remove the "yield" line from inside the loop, you'll hard-freeze Unity, because it will never escape the loop.)

    To start this, call StartCoroutine(DropAttack()); .... Make sure you only call this once (e.g. in Start()). If you call it repeatedly (e.g. in Update(), if you don't have conditions on it to keep it from being called more than once), you'll get hundreds and hundreds of these coroutines running in parallel.
     
  3. JustaDuck97

    JustaDuck97

    Joined:
    Jul 31, 2019
    Posts:
    45
    Thank you very much that makes a lot of sense; now i'm just wondering why I couldn't figure that out on my own.
     
  4. davidnibi

    davidnibi

    Joined:
    Dec 19, 2012
    Posts:
    424
    I had the while freeze before - I also forgot to save my scene.
    Why does while(true) return as true in this case?
     
  5. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    A infinite while loop in a Coroutine will cause a freeze. Make sure that you are yielding somewhere in the while loop.
     
  6. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,684
    As a personal note and to reduce garbage, NEVER create a new "WaitForSeconds" every loop. Create a reference and reuse it

    Code (CSharp):
    1.         private WaitForSeconds refreshIntervalWait = new WaitForSeconds(3);
    2.         private IEnumerator DropAttack()
    3.         {
    4.              while (true)
    5.             {
    6.                 yield return refreshIntervalWait;
    7.                 GameObject TrapAttack = Instantiate(SpikerPrefab, Dropper.position, Dropper.rotation);
    8.             }
    9.         }
     
    shmemadt and Jordylicious like this.