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!

3 Answers

3
var onetime = false;

Update()
{
  if (!onetime)
  {
    Activate();
    onetime = true;
  }
}

no problem, happy to help

Also Awake() can be used

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)

Start() is called at the very begining, just before the first Update() call

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;
}
}

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
}