JS to C# converting issues..

Hello all, I am creating a level in unity for a uni assignment; I followed this 3part tutorial a few months back: Unity3d Enemy Wave Spawn Script [PART 3] - YouTube and got it working. The problem was when I tried converting it to a C# script so that I could use it with the PhotonNetwork.

It is working for the most part; there are no errors but for some reason the spawn times are off and all enemies after the first spawn faster than the random.range(yieldTimeMin, yieldTimeMax)
which is between 1-3 seconds.
I will post my JavaScript code and also my C# code(i’m not used to using for loops and yields in C# and was told something about invoke repeating would be a good idea too so if that helps then I’d love to know how I would word it into the script.


#pragma strict

//This Script is dealing with all of the enemy spawning such as how often, where, wave numbers etc.
//It is placed onto the object that holds all of the spawnpoints in the game (Holder_EnemySpawnPoints).
//@HideInInspector to hide the variable on the next line from the Inspector.

var enemySpawnPoints : Transform[];
var enemyPrefabs : GameObject[];

var yieldTimeMin : float = 1;
var yieldTimeMax : float = 3;

var spawnXOffsetMin : float = 0;
var spawnXOffsetMax : float = 0;
var spawnZOffsetMin : float = 0;
var spawnZOffsetMax : float = 0;

var enemyCounter : int = 0;
var startingSpawnNumber : int = 4;
var isSpawning : boolean = false;

var waveNumber : int;

function SpawnEnemies(wave : int)
	var spawnNum = (startingSpawnNumber + 3 * (waveNumber - 1));
	isSpawning = true;
	for(var i = 0; i < spawnNum; i++)
		yield WaitForSeconds(Random.Range(yieldTimeMin, yieldTimeMax));
		var spwnObj : GameObject = enemyPrefabs[Random.Range(0, enemyPrefabs.Length)];
		var spwnPos : Transform = enemySpawnPoints[Random.Range(0, enemySpawnPoints.Length)];
		Instantiate(spwnObj, spwnPos.position + Vector3(Random.Range(spawnXOffsetMin, spawnXOffsetMax), 0, Random.Range(spawnZOffsetMin, spawnZOffsetMax)), spwnPos.rotation);
	isSpawning = false;

function UpdateWave()

function Start()
	waveNumber = 1;

function Update()
	if(enemyCounter == 0 && !isSpawning)

congratulations for formatting the code properly. it sometimes seems like a lost art :wink:

add some Debug.Log’s into it to determine the random time.

you don’t need enemyCounter++; in SpawnTime() AND SpawnEnemies()

in SpawnEnemies() you’re not using the parameter that you pass…

Start() could just have waveNumber = 0; - the spawning will be done in the first Update() cycle.

InvokeRepeating() could work for regular intervals but since you want random times, it’s not relevant here.

and there’s nothing to stop the same spawn positions and objects being used in this code. is that intentional?