- Home /
How I can call a function once on the "Update" function?
I have a function:
function Activate()
{
...
}
How i can call the function Activate one time in the Update? In javascript please.
function Update() {
Activate() // Once time only
}
Thank you!
Answer by DaveA · Mar 16, 2011 at 08:42 PM
var onetime = false;
Update() { if (!onetime) { Activate(); onetime = true; } }
True that! Just yesterday I answered someone looking to do 'start' after a delay, which is where my head was at answering this one. Presumably there would be an additional condition, like if (!onetime && some_important_other_thing_happened)
Answer by jquery404 · Jun 09, 2012 at 07:45 AM
you could call Invoke() too, if u want some delay before calling ur Activate() function. like this
void Update() { // it will call Activate function after 1 sec. if (!onetime) { Invoke("Activate", 1.0); onetime = true; } }
Answer by The1nk · Jun 11, 2012 at 05:02 AM
Update()
fires on every frame. You wouldn't even want that If()
statement in there.
Instead, use the Start()
function -- it only fires when the script starts.
function Start()
{
// This is only called once
}
function Update()
{
// This is called every frame
}