- Home /
How to create a UDK-like timer to call a function ?
Hello,
I've started using Unity recently and I'm trying to "find my marks" after working on UDK for 6 months. One very useful function of UDK is a timer : basically it allows you to call a function after a specific amount of time has elapsed. Is there anything similar to this in Unity ? I've seen a few threads but they all seem to involve tinkering with the Update() function. I'm talking about something really simple which is called like this : "SetTimer(time, repeat, nameOfFunction). Repeat basically means whether it has to be called once or if it should be called again after each call. (say, you wanted to call a function every second for the duration of the program)
In my case, I want to use it (among other things) for an "AttackSpeed" statistic for my unit : the unit should attack its target X times per second, where X is its attack speed (so if it has an attackspeed of 1, then it attacks once per second, twice with an AS of 2, etc).
Thanks in advance for your help. (Also I hope that referencing other engines is not prohibited here because that's really the easiest way for me to give a reference to what I mean)
Answer by Dave-Carlile · Oct 29, 2013 at 04:01 PM
You can use the Invoke function to do this. It accepts a timer value and will call the function after that amount of time.
There is also InvokeRepeating, which will invoke the function repeatedly at your specified interval.
Dave, thanks for the response. FYI that first link is broken. Should be Invoke.
heh - thanks for the notice. Guess Ctrl+C didn't work and you got the last link I copied. Fortunately it wasn't anything embarassing.
Your answer
Follow this Question
Related Questions
Problem with Time.time 0 Answers
How to make a stopwatch that restarts every time the player dies and saves it as a highscore 3 Answers
Are there any performance differences between a timer and using Invoke? 1 Answer
Trying to get Timed button presses within a timed window 1 Answer
Timer Data Across Scene Changes 1 Answer