add delay to enemy spawning script

I have a enemy spawning script that instantiates an enemy on the spawnpoint that the script but it spawns an enemy evey tick wich increases lag and I don’t want it to cosntantly spawn an emeny every tick

This is what I got

function update () {
//spawn enemy
}

I wnated to change it to spawn an emeny every few seconds maybe 5 seconds, how would I do this

@ EliteHedgehog56 hello! it seems that youve not uploaded full code so I ll give you some hint use this in code else uploaded full your`s enemy spawning script hint is :

WaitForSeconds(5f);//wait for 5 seconds
//write here code for spawning your enemy

it will work definitely

oh sorry I Should of you’re right

this is my script

var enemy : Transform;

function Update () {

       Instantiate (enemy,transform.position,transform.rotation);

}

@EliteHedgehog56 Well, this should do the trick…

In your Update() function:

    StartCoroutine(SpawnEnemy());

Create an SpawnEnemy() function:

function SpawnEnemy()
{
     yield WaitForSeconds (5f);

     // Your code to spawn the enemy
     Instantiate (enemy,transform.position,transform.rotation);
}

All the best :slight_smile:

Don’t start Coroutine in Update

    #pragma strict
    
    var enemy : Transform;
    
    function Start () 
    {
    	yield StartCoroutine(SpawnEnemy());
    }
    
    function SpawnEnemy()
    {
    	while (true) {
    		
    		Instantiate(enemy, transform.position, transform.rotation);
    	    yield WaitForSeconds(5f);
    	}
    }