How to control time?

I’m new at unity,I’m liking the engine too much,but I have a problem about the time control,for example if I want to order a game object to do something at each 2 seconds can I only do this with yield?If yes,why with me this doesn’t works sometimes?I was creating a bomber man clone on the last 2 hours,that’s my script for the bomb:

var bombs:GameObject;
var ready:boolean=true;
function Update () {

if(Input.GetButtonDown("Fire1"))
{

DragBomb();
}

}

function DragBomb()
{
if(ready==true)
var bomb=Instantiate(bombs,transform.position,transform.rotation);
ready=false;
yield WaitForSeconds(3);
ready=true;
}

Works nice if I press ctrl not too fast,but if I press ctrl many time the 3 seconds are not respected.Is this a logical problem on my script or yield’s not the best option for these things?

Sorry for my bad English,I’m Brazilian.

try putting a bool in there. When you start the DragBomb function set it to true; Then above the DragBomb() in your update only do it if that value is false. Lastly set it to falst after your yield

Logical problem in your code… no

But for what you want to do yield probably isn’t a good method. Yield basically insures that what ever function you have it in will be reactivated where you have the yield after the time you tell it to wait for has passed, it does not insure that the function it is in will not be called again (as you’re finding out.)

Probably be a better idea to do this completely in the Update function and with a timer.

some thing like:

 var MaxTime : float; // your 3 seconds, or what ever other time you might want to assign in the inspector.
private var timer = 0.0; // Make a timer variable;
private var RunTheTimer = false; // Should the timer move forward?
function Update()
{
     if(button pressed) RunTheTimer=true; // put in what ever button code you want for button pressed

     if(timer>=MaxTime)
     {
          RunTheTimer=false;
          timer = 0.0;
     }
     else if(RunTheTimer)
     {
          // Do here what ever code you want to run for MaxTime, after button pressed is true.
          timer +=Time.deltaTime;
     }
}

More info on Time.

I would just make a function that toggles your ready var and invoke it

Oh thanks to much for everyone who helps:smile:
Now I can continue my journey on gamedev,thank to much