x


timed events...

Hello. How can I have a static variable be set to a specific value after a set amount of time? I dont want to use Invoke Repeating because the values will not be based on the same formula (ex: it wont be something like "every 2 seconds add 5"). For example, after 30 seconds the value will equal 5, after 45 it will equal 20 and after 60 it will equal 15 etc. Thanks

more ▼

asked Dec 31 '10 at 08:23 PM

Tyler 2 gravatar image

Tyler 2
1.1k 211 246 264

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

There are several methods, but the easiest way is to have a interval variable and a timer variable. Lets say we want to trigger something every second:

var interval:float = 1; //for 1 second
var timer:float;

now:

function Start()
{
  timer = Time.time + interval; //the timer is equal to the time in the future
}
function Update()
{
  if(Time.time >= timer) //if the current time elapsed is equal to or greater than the timer
  {
    //do something
    timer = Time.time + interval; //set the timer again
  }

}

again, there are many ways to do it but this is a quick and dirty way I use often.

more ▼

answered Dec 31 '10 at 10:03 PM

kennypu gravatar image

kennypu
677 12 12 24

Exactly what I was looking for, many thanks!

Jan 05 '11 at 12:57 AM Socapex
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x819
x570

asked: Dec 31 '10 at 08:23 PM

Seen: 1850 times

Last Updated: Dec 31 '10 at 08:23 PM