Shot in each 2 seconds?

I’m developing an small space game,the enemy has a super simple AI that basically is to shot at each 2 seconds,I tried to do this:

var shotE:GameObject;
function Update () {
shot();
}
function shot()
{
yield WaitForSeconds(2.0);
var balaj=Instantiate(shotE,transform.position,transform.rotation);
}

But it’s shoting like a crazy:shock:,how can I fix this bug?

I bashed this together really quickly.
I hope it works!

var timer = 0;
var shotE : gameObject;

function Update () {
	timer += 1 * Time.deltaTime;
	if(timer >= 2) {
		var balaj = Instantiate(shotE, transform.position, transform.rotation);
		timer == 0;
	}
}

It’s not shoting anymore:(

If you don’t need any extra features, you can use InvokeRepeating.

var shotE : GameObject;

function Start () {
    
    InvokeRepeating ("shot", 2, 2);
    
}

function shot () {
    
    var balaj=Instantiate(shotE,transform.position,transform.rotation);
    
}

Damn , i came to answer this with a simple script , but , DMan , you have taught me something again, didnt even know about that Method :stuck_out_tongue: TYVM.

Thanks for the help man:smile:

Sorry, I’d forgotten a line in the code that I gave. Oops.

Dman, thanks for your help - I’d never known about InvokeRepeating, and am going to change several of my current project’s scripts to use it. It certainly cuts down on the number of lines!