Trying to write enemy spawn in JS.

I’m very new to programming, so far this is what I have;

var enemyType   : Transform;
var enemySocket : Transform;


function Update ()
{
	if (Time.time >= 5)
	{
	Spawn ();
	}
}

function Spawn ()
{
	InvokeRepeating("enemyType", 5, 5);
	Instantiate(enemyType, enemySocket.position, enemySocket.rotation);
	CancelInvoke ();
}

Obviously, I’m missing something here, as it spawns a continuous stream of enemys and will not stop. I’m trying to get this code to a point where it can start and stop after various time intervals. (ex: spawn 4 enemies after 1 minutes, then spawn another 4 30 seconds after that and so on and so forth)

any help would be appreciated

well, Hello World to you. :slight_smile: You are probably spawning an incredible amount by the looks of it =D… In Update() you are calling Spawn() every frame at 5 seconds and every frame after that. Then inside spawn you are making all those repeat every 5 seconds… If you mean to spawn once every 5 seconds, you need to have a variable to record how much time has passed, then reset it when you spawn your enemy. For example:

Note, i never use javascript, so not sure this will work directly, i’ll try to guess it tho… There are many better ways to do this, but using your code and experience, you can do this:

 var spawnTime : float = 0.0f;
 var turnOffUpdate1 : bool = false;
 var maxTime : float = 500.0f;
  function Update() {
     if (spawnTime >= 30.0f && !turnOffUpdate1) {
        Spawn();
        turnOffUpdate1 = true;
     }
     if (spawnTime >= 300) { //i.e. reached 4 minutes and haven't done it yet
       Spawn();
       turnOffUpdate1 = false;
       spawnTime = 0.0f;
     }
     spawnTime += Time.deltaTime; 
    }
    
 function Spawn() {
 //you can keep it like you have i think, but why invokerepeating then cancel? Just instantiate an enemy once here, forget the invoke repeating calls
}

So you see, it has to record how much time has passed, then only execute once it has. (after 30 seconds the first time)… the next one will spawn at around 4 minutes, then it will reset, and spawn after 30 seconds then 4 minutes again, rinse repeat.
this is just one (not very good way) to do it. you likely will want a dictionary or array or list or something that will know spawn times later, but i don’t know how new you are, you hopefully should be able to grasp this.

Thanks so much for the help! I was able to plug this in an get it running, thank you so much! Could you possibly explain what && !turnOffUpdate is doing? I’ve figured out the rest of what you have done but I am uncertain about that line.

Thanks again!