Doing an action only once

Hi community,

It’s a simple problem, but I can’t think of a good solution. In my game I’m instantiating a prefab when time.Time is equal to a certain variable. My issue is that I’m rounding this, and when I call the function during update, it calls it many times in that 1 second period (because time is rounded to 1 for many updates). Currently I have an if statement on the timer, but I only want to call a function once. I don’t want to create a boolean or something (as an on/off switch) because I will be calling this function hundreds of seperate times (thus hundreds of on/off variables). So I need to answer one of these two questions.

How can I make multiple functions in a single if statement run once each (without unique on/off switches)?

or

How can I get an exact period of time.Time (since frame rate will give me different values each run through)?

Thanks in advance.

Maybe something like this?

var countdown : float = 5.0;

function Update()
{
   if (countdown > 0)
   {
      countdown -= Time.deltaTime;
      if (countdown <= 0)
      {
         // DO THE ONE TIME THING!
         enabled = false; // Optional: disables further updates on this object.
      }
   }
}

Don’t use Update, use coroutines.

function Start () {
   yield WaitForSeconds(waitTime);
   // Do the thing here
}

–Eric

Agreed! That’s even easier.

Okay I got something working based off of that. I call the coroutine once and then in that coroutine I use the yields to instantiate the prefabs at the time I want.

Thanks!