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

Calling a function every 2 seconds.

Discussion in 'iOS and tvOS' started by numeric, Aug 30, 2010.

  1. numeric

    numeric

    Joined:
    Jan 31, 2010
    Posts:
    170
    Does anyone know a good way I can call a function every two seconds so I can control the speed at which I my Ball objects across the screen.

    I already have a function to instantiate the objects I just need a way to call it at specified time intervals , is it possible to make these values a little varied using something like Random.Range?
     
  2. jingato

    jingato

    Joined:
    Jun 23, 2010
    Posts:
    299
    what is it exactly that your ball is doing? Is it something bouncing around the screen?
     
  3. Moonjump

    Moonjump

    Joined:
    Apr 15, 2010
    Posts:
    2,572
    You could do something like this:

    Code (csharp):
    1. private var gapTest = true;
    2.  
    3. function Update () {
    4.     if (gapTest) {
    5.         // do your stuff here option 1
    6.         GapSet ();
    7.     }
    8. }
    9.  
    10. function GapSet () {
    11.     gapTest = false;
    12.     // do your stuff here option 2
    13.     yield WaitForSeconds (2);
    14.     gapTest = true;
    15. }
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    InvokeRepeating("MyFunction", .01, 2.0);

    Code (csharp):
    1. function Start () {
    2.     while (true) {
    3.         yield WaitForSeconds(Random.Range(1.0, 2.0));
    4.         // do something
    5.     }
    6. }
    --Eric
     
  5. numeric

    numeric

    Joined:
    Jan 31, 2010
    Posts:
    170
    Thanks very much for the advice guys, I took it all on board and it seems to be working nicely :D
     
  6. jtbentley

    jtbentley

    Joined:
    Jun 30, 2009
    Posts:
    1,397
    Eric, is there any reason you wouldn't simply do it like this..

    Code (csharp):
    1.  
    2. function Start()
    3. {
    4.  gapSet();
    5. }
    6.  
    7. function gapSet()
    8. {
    9.  while(true)
    10.  {
    11.  yield WaitForSeconds(Random.Range(1.0,2.0);
    12.   // Do stuff
    13.  }
    14. }
    15.  
    While i'm sure the performance difference between the two isn't even measurable, I'm just curious as to why you've done it that way :)
     
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Considering they are exactly the same, I'm sure too. ;)

    Takes less time to type, uses a few bytes less memory....

    --Eric
     
  8. felixwcf

    felixwcf

    Joined:
    May 22, 2012
    Posts:
    16
  9. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
  10. felixwcf

    felixwcf

    Joined:
    May 22, 2012
    Posts:
    16
    Holy Molly sorry yea it did exist since 2010... (i've been an dumbass for nearly 6 years...)
     
  11. dreek16designer

    dreek16designer

    Joined:
    Mar 21, 2020
    Posts:
    8
    Well You can just invoke the function in Update method:

    void Update ()
    {
    Invoke("Ball", 2f);
    }

    I didn't know what you function was called, so replace "Ball" with your function name.