- Home /
How to call a routine every 3 seconds?
Just wondering if there is an equivalent to Objective-C's "NSTimer" to allow you to call a routine every x amount of seconds? I've tried looking at WaitForSeconds but I'm not 100% sure what it's doing because it doesn't seem to work.
Thanks in advance!
Answer by Mike 3 · Jun 18, 2010 at 04:20 PM
you can just use this:
InvokeRepeating("YourFunctionName", 3);
or (js):
function Start() { YourFunctionName(); }
 
               function YourFunctionName() { while(true) { DoSomething(); yield WaitForSeconds(3); } } 
or (c#):
using System.Collections;
 
               public class YourClass {
  void Start()
 {
     StartCoroutine(YourFunctionName());
 }
 IEnumerator YourFunctionName()
 {
     while(true)
     {
         DoSomething();
         yield return new WaitForSeconds(3);
     }
 }
 } 
@$$anonymous$$ike, you could go ahead and put the Using statement into the code block, before Start - it doesn't have to be at top, just before any usage. (And Unity adds that line at top by default anyway, when you create a C# script).
I did, it just went a bit wonky, I'll try edit it back in :P CommentEdit!: There, better :)
Yeah, sometimes the Code block flakes out on the first few lines, dunno why. Hope you don't $$anonymous$$d, I did go ahead and fix it so it wasn't a comment. :)
Where do I put the code I want to repeat in (C#) and where do you declare DoSomething?
Answer by synapsemassage · Nov 10, 2010 at 03:17 PM
correct Javascript syntax for InvokeRepeating would be: function InvokeRepeating (methodName : string, time : float, repeatRate : float) : void
example: InvokeRepeating("YourFunctionName", 0, 3);
"0" is the delaytime.
Your answer
 
 
             Follow this Question
Related Questions
Object generator? 1 Answer
Can I use unity 3d as a component of UI in ipad app? 0 Answers
Rotating doesn't work correctly? 2 Answers
Menu with 3D-objects - using swipe on iPhone 1 Answer
iPhone game with free Unity? 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
               
 
			 
                