Hello community ![]()
I have a really big problem here since it totally prevents me from working on my game…
Here it is:
I have a game that spawnes enemys when i press Enter. All the scripts used for this are these:
The GameManager controlls how many rounds have passed and checks if i press Enter:
public static int rounds = 1;
public static bool roundIsRunning = false;
void Update () {
if (roundIsRunning == false && Input.GetKeyDown (KeyCode.Return)) {
Debug.Log("Round startet");
Debug.Log("This is round " + rounds);
rounds++;
roundIsRunning = true;
EnemySpawner.activeSpawner.spawnEnemy(rounds);
}
}
The EnemysSpawner spawns all the enemys when called:
private bool spawnNext = true;
public Transform defSpawn;
public GameObject enemyPrefab;
public float maxOffset;
public float spawnRate;
public int remaining = 0;
public int enemysOnField = 0;
public static EnemySpawner activeSpawner;
void Awake() {
//Creating a static instance to be called form other scripts
if (activeSpawner == null)
activeSpawner = this;
else if (activeSpawner != this)
Destroy (gameObject);
}
public void spawnEnemy(int rounds){
//Loop variable to Debug.Log how many loops have passed
int loop = 0;
Debug.Log("Enemys spawning...");
//Remaining is the count of enemys based on the round you are in
remaining = (int)Mathf.Log (rounds, 2f);
Debug.Log ("Total enemys: " + remaining);
//Spawning all the enemys in this loop
while (remaining > 0) {
loop++;
if (spawnNext == true) {
Instantiate (enemyPrefab, getSpawnPos(), Quaternion.identity);
remaining--;
Debug.Log("Spawned an enemy. Remaining:" + remaining);
enemysOnField++;
spawnNext = false;
//Starting a coroutine so that there is a little time in between spawns
StartCoroutine(waitForNextEnemy());
}
Debug.Log("Looped " + loop + " times");
}
Debug.Log ("All enemys spawned");
GameManager.roundIsRunning = false;
Debug.Log ("Round is over");
}
public Vector3 getSpawnPos(){
Vector3 spawnPos = defSpawn.position;
spawnPos.y += Random.Range (-maxOffset, maxOffset);
return spawnPos;
}
IEnumerator waitForNextEnemy() {
Debug.Log ("Waiting till next spawn...");
yield return new WaitForSeconds (0.5f);
spawnNext = true;
}
I beg you to read through this and tell me what is wrong.
I have already had some problems with coroutines but i dont know how this could be an issue here…
Also I should mention that it only freezes on the 3rd time I press Enter. The first 2 times it works perfectly fine.
In addition it freezes while still in the GameManager script. I can tell because it wont even show the first Debug.Log message. It freezes before that ![]()
I hope you guys can help me because this is starting to piss me off ![]()
Thanks in advance ![]()