Hello Everyone!
I’m working on a 3D game inspired by Space Invaders, and for the past week I’ve been having an issue with the enemy spawning function, controlled by a script attached to the EventSystem. I have used Debug.Log() statements, and from what I could gather, the issue is that after the first few enemies are killed the wave is somehow increasing exponentially. This triggers sudden increases in the enemiesToSpawn variable (values upwards of 600) and mass spawns enemies. I’ve attached the script below, the relevant areas are Start(), Update(), SpawnEnemy(), and enemiesToSpawnZero(). I would really appreciate any help, even if it’s just a nudge in the right direction! Thank you in advance!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using TMPro;
public class EventScript : MonoBehaviour
{
public Button pause;
public GameObject window;
public Button resume;
public Button backToHub;
public Button settings;
public TextMeshProUGUI score;
public TextMeshProUGUI waveText;
string scoreNumber;
public GameObject Enemy;
public Slider speedControl;
public GameObject rightBton;
public GameObject leftBton;
public Button Return;
public int enemiesToSpawn = 0;
public int wave = 0;
float RandomXValue;
public Camera deathCamera;
public Camera mainCamera;
public GameObject redHaze;
public GameObject healthText;
public int enemiesKilled = 0;
public float delayTime = 3.0f;
public int health = 3;
void Start()
{
score = score.GetComponent<TextMeshProUGUI>();
pause.onClick.AddListener(Pause);
resume.onClick.AddListener(Pause);
settings.onClick.AddListener(OpenSettings);
Return.onClick.AddListener(Resume);
window.SetActive(false);
deathCamera.gameObject.SetActive(false);
redHaze.SetActive(false);
Invoke("newWave", 3.0f);
enemiesToSpawnZero();
}
void Update()
{
rightBton.GetComponent<SpaceshipRight>().speed = speedControl.value;
leftBton.GetComponent<SpaceshipLeft>().speed = speedControl.value;
score.SetText("Score: " + enemiesKilled.ToString());
if (enemiesToSpawn == 0 && enemiesKilled > wave * 2 - 1)
{
Invoke("enemiesToSpawnZero", delayTime);
}
if (enemiesToSpawn != 0)
{
float randomEnemyTimer = Random.Range(2.0f, 6.0f);
Invoke("SpawnEnemy", randomEnemyTimer);
enemiesToSpawn--;
}
if(health == 0)
{
death();
}
Debug.Log("Enemies to spawn: " + enemiesToSpawn);
Debug.Log("Wave: " + wave);
}
public void Pause()
{
if (window.activeInHierarchy)
{
window.SetActive(false);
Time.timeScale = 1;
}
else
{
Resume();
}
}
public void Resume()
{
window.SetActive(true);
settings.gameObject.SetActive(true);
backToHub.gameObject.SetActive(true);
resume.gameObject.SetActive(true);
Return.gameObject.SetActive(false);
Time.timeScale = 0;
}
public void OpenSettings()
{
resume.gameObject.SetActive(false);
settings.gameObject.SetActive(false);
backToHub.gameObject.SetActive(false);
Return.gameObject.SetActive(true);
}
public void SpawnEnemy()
{
RandomXValue = Random.Range(-100.0f, 100.0f);
Enemy.SetActive(true);
Instantiate(Enemy, new Vector3(RandomXValue, Enemy.transform.position.y, 130), Quaternion.identity);
Enemy.SetActive(false);
}
public void death()
{
healthText.SetActive(false);
score.gameObject.SetActive(false);
mainCamera.gameObject.SetActive(false);
deathCamera.gameObject.SetActive(true);
redHaze.SetActive(true);
Time.timeScale = 0.3f;
}
public void newWave()
{
waveText.gameObject.SetActive(false);
}
public void enemiesToSpawnZero()
{
wave++;
delayTime += 1.2f;
waveText.gameObject.SetActive(true);
waveText.SetText("Wave " + wave.ToString());
Invoke("newWave", 3.0f);
enemiesToSpawn = wave * 2;
}
}