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

c# how to settimeout ienumerator call every 3 second on runtime

Discussion in 'Scripting' started by Sekai92, Sep 14, 2015.

  1. Sekai92

    Sekai92

    Joined:
    Apr 22, 2015
    Posts:
    32
    sir how to settimeout ienumerator call every 3 second on runtime? So every 3 second will call same ienumerator function. Thanks
     
  2. Craig8726

    Craig8726

    Joined:
    Jul 5, 2013
    Posts:
    79
    Code (CSharp):
    1. void Start()
    2.     {
    3.         StartCoroutine ("loopingDelay");
    4.     }
    5.     IEnumerator loopingDelay()
    6.     {
    7.         yield return new WaitForSeconds (3);
    8.         StartCoroutine("loopingDelay");
    9.     }
    make sure you only initialise it once not every frame.
     
    Sekai92 likes this.
  3. Sekai92

    Sekai92

    Joined:
    Apr 22, 2015
    Posts:
    32
    Is this good to make function like this on every game object?
    I have 10 game object and i try to run that class on every game object.
     
  4. Craig8726

    Craig8726

    Joined:
    Jul 5, 2013
    Posts:
    79
    yes thats should be fine as long as its in start() or some other method that is only called on one frame. if in update() it will start the coroutine every frame and defeat the purpose of the delay.
     
    Sekai92 likes this.
  5. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Start can be a coroutine itself

    Code (csharp):
    1.  
    2. IEnumerator Start()
    3. {
    4.     while (true)
    5.     {
    6.         yield return new WaitForSeconds(3);
    7.         DoSomething();
    8.     }
    9. }
    10.  
     
    sluice likes this.
  6. Sekai92

    Sekai92

    Joined:
    Apr 22, 2015
    Posts:
    32
    oh thank you for the explaination sir very helpful for me