Waiting then Executing

var bullet : Transform;

function Update(){
	waitNGO();
}

function waitandgo()
{
	clone = Instantiate (bullet, transform.position, transform.rotation);
	yield WaitForSeconds(2);
	Destroy(clone.GameObject);
}

function waitNGO()
{
	yield waitandgo();
}

Here is my code. What is does is sprays endless ammounts of bullets (alot per frame). I do not know how to fix it. I have been to countless places, and needless to say no one has answered it to which i can find the answer. Thanks for your help :slight_smile:

2 ways either you use GetButtonDown and you need to press each time or use a timer:

first way

function Update()
{
    if(Input.GetButtonDown("Jump"))
    {
        clone = Instantiate (bullet, transform.position, transform.rotation);
        Destroy(clone.gameObject,2.0f);
    }
}

second way is shooting every 2 seconds as you keep the button down:

var timing:int;

function Update()
{
    timing = Time.time %2;
    if(Input.GetButton("Jump"))
    {
        if(timing)
        {
            clone = Instantiate (bullet, transform.position, transform.rotation);
            Destroy(clone.gameObject,2.0f);
        }
    }
}

By the way you don’t need to yield and destroy. The second argument passed to the destroy function is a timer.

Sure you call the function every Update so it’s called every frame. Your whole coroutine “mess” effectively does this:

var bullet : Transform;

function Update(){
    clone = Instantiate (bullet, transform.position, transform.rotation);
    Destroy(clone.GameObject, 2);
}

It seems you want to slow down the instantiate rate. You can use a single coroutine for that which means it has to be started only once. Something like that:

var bullet : Transform;
var spawnDelay = 2.0;         // a bullet every 2 sec
var maxBulletLifeTime = 10.0; // destroy the bullet after 10 sec.

function Start()
{
    SpamBullets(); // Start the coroutine
}

function SpamBullets()
{
    while(true)
    {
        clone = Instantiate( bullet, transform.position, transform.rotation );
        Destroy( clone.GameObject, maxBulletLifeTime );
        yield WaitForSeconds( spawnDelay );
    }
}

You put an yield inside waitNGO function, but that’s not sufficient: function Update continues to operate every frame. As a result, you have a bunch of calling to the wait NGO function: each of them will wait using the yield, but all of them will be already “queued” for the execution, and the “wait” is nullified.
You can solve introducing a variable (lock) that controls when waitNGo has finished or not its execution.

var bullet : Transform;
var lock = false;

function Update(){
   if (!lock)
      waitandgo();
}

function waitandgo()
{
    lock = true;
    clone = Instantiate (bullet, transform.position, transform.rotation);
    yield WaitForSeconds(2);
    Destroy(clone.GameObject);
    lock = false;
}

function Update(){
if(Input.GetButtonDown(“Jump”)
{
clone = Instantiate (bullet, transform.position, transform.rotation);
Destroy(clone.gameObject,2.0f);
}
}

var timing:int;

function Update()
{
    timing = Time.time %2;
        if(Input.GetButton("Jump")){
            if(timing){
                clone = Instantiate (bullet, transform.position, transform.rotation);
                Destroy(clone.gameObject,2.0f);}
            }
        }
   }

And i also see that you forgot a } in the process